我正在使用以下代码来创建zip文件夹。我的zip文件夹已经创建了,但是当我们提取此zip文件夹时,就会丢弃一些错误: - 找不到档案。
try {
File inFolder = new File("D:\zextra\ab");
File outFolder = new File("D:\zextra\ab.zip");
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFolder)));
BufferedInputStream in = null;
byte[] data = new byte[1000];
String files[] = inFolder.list();
for (int i = 0; i < files.length; i++) {
in = new BufferedInputStream(new FileInputStream(inFolder.getPath() + "/" + files[i]), 1000);
out.putNextEntry(new ZipEntry(files[i]));
int count;
while ((count = in.read(data, 0, 1000)) != -1) {
out.write(data, 0, count);
}
out.closeEntry();
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
我认为您也应该关闭FileOutputStream
。zipoutputstream关闭可能还不够。
参见例如http://www.journaldev.com/957/java-zip-example-to-zip-single-file-file-and-and-a-directory-recursishife
他们使用fos.close();