由 java.util.zip 压缩的文件无法通过标准 zip 实用程序解压缩

发布于 2024-12-16 16:58:31 字数 973 浏览 0 评论 0原文

我设法使用这段简单的代码在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();

zip文件创建成功。但是,当我尝试使用 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 没有显示任何错误。

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

I managed to create zip file in java using this simple piece of code:

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();

zip file created successfully. However when I try to unzip it using WinZip or some other tools I get an error:

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.

What's really weird, WinRAR and internal Win7 zip do not show any errors when I decompress the file.

What am I doing wrong? Anybody had this issue?
Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-12-23 16:58:31

一定是 out.close 丢失了。

Must be that out.close is missing.

暗喜 2024-12-23 16:58:31

我认为您忘记关闭 zipentry。

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

I think you forgot to close zipentry.

out.closeEntry();
origin.close();
out.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文