使用java.util.zip压缩的文件无法被标准zip工具解压缩。

3

我使用以下简单的Java代码成功创建了zip文件:

BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(zipFile);

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
out.setMethod(ZipOutputStream.DEFLATED);
out.setLevel(5);

byte data[] = new byte[BUFFER];
FileInputStream fi = new FileInputStream(file);
origin = new BufferedInputStream(fi, BUFFER);

ZipEntry entry = new ZipEntry(file.getName());
out.putNextEntry(entry);

int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
    out.write(data, 0, count);
}

out.closeEntry();
origin.close();
out.close();

压缩文件已成功创建。但是,当我尝试使用WinZip或其他工具解压缩时,出现错误:

Central and local directory mismatch for file "my_file" (general 
purpose flags - local:808 hex  central: 8 hex). 
Severe Error:  Local and central GPFlags values don't match.

非常奇怪,WinRAR和Win7内置的zip解压文件时没有显示任何错误。

我做错了什么?有人遇到过这个问题吗? 谢谢!


发现相关问题,我在Android 2.3中也使用Java:http://stackoverflow.com/questions/7407695/android-2-3-zip-problems-with-general-purpose-flags - undefined
2个回答

1

可能是缺少 out.close。


0
我觉得你忘记关闭zipentry了。
out.closeEntry();
origin.close();
out.close();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接