打开使用 java 创建的 zip 文件时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4067021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:39:03  来源:igfitidea点击:

Error opening zip file created using java

javazipoutputstream

提问by Anoop

I created a small application to read some files from the disk and zip it using java.util.zip.ZipOutputStream. It is successfully creating the zip file. But in windows when i try to open it / extract it am getting the error message like "Windows has blocked access to these files to help protect your computer". I am zipping only csv files. But if i try to unzip using the zipinputstream class from java itself, its unzipping it correctly. Can anyone throw some light on it.

我创建了一个小应用程序来从磁盘读取一些文件并使用 java.util.zip.ZipOutputStream 将其压缩。它已成功创建 zip 文件。但是在 Windows 中,当我尝试打开它/解压缩它时,会收到错误消息,例如“Windows 已阻止访问这些文件以帮助保护您的计算机”。我只压缩 csv 文件。但是,如果我尝试使用 java 本身的 zipinputstream 类解压缩它,它会正确解压缩它。任何人都可以对此有所了解。

regards, Anoop

问候, Anoop

回答by Anoop

Finally I found out the problem. It was related to the path. its really funny, but if u give the absoute path of the files to be zipped to zipoutputstream, this error happens. i tried with relative paths and BINGO!!! it worked. Hence i did some work around before zipping and pointed the parent of the files to the current working directory and then zipped. Thanks all for the responses.

最后我发现了问题所在。这与路径有关。这真的很有趣,但是如果您提供要压缩到 zipoutputstream 的文件的绝对路径,则会发生此错误。我尝试使用相对路径和宾果游戏!!!有效。因此,我在压缩之前做了一些工作,并将文件的父级指向当前工作目录,然后进行压缩。感谢大家的回应。

回答by Chris

I know this post was several years ago. However, I hit something very similar in just using java.util.zip for the first time, and this post lead me to resolve the issue.

我知道这篇文章是几年前的。但是,我在第一次使用 java.util.zip 时遇到了非常相似的问题,这篇文章引导我解决了这个问题。

Anoop's last comment about absolute paths helped me find the problem. Since I didn't see the answer in searching several posts, I wanted to post it here - actually responding to Roland's last question:

Anoop 关于绝对路径的最后一条评论帮助我找到了问题所在。由于搜了好几个帖子都没有看到答案,所以想贴在这里——其实是在回答罗兰的最后一个问题:

The issue was when I used ZipEntry(file) with a fully qualified path/file, instead of a relative path. I couldn't open the resulting ZIP with any of my Windows OS instances. However, I could extract the file again with Java. It wasn't until I opened the zip with 7zip that I realized the issue. The first folder in my result.zip file was "D:". My directory was a long path under my D drive. So when opening my "Results.zip" file, here's what I would see after clicking down the directory tree in the zip file (from 7zip): Results.zip\D:\Apps\vertigo\instance5\runtime\myManager\discoveryResources\data

问题是当我使用带有完全限定路径/文件的 ZipEntry(file) 而不是相对路径时。我无法使用我的任何 Windows 操作系统实例打开生成的 ZIP。但是,我可以使用 Java 再次提取该文件。直到我用 7zip 打开 zip,我才意识到这个问题。我的 result.zip 文件中的第一个文件夹是“D:”。我的目录是我的 D 盘下的一个很长的路径。因此,当打开我的“Results.zip”文件时,在单击 zip 文件(来自 7zip)中的目录树后,我会看到以下内容: Results.zip\D:\Apps\vertigo\instance5\runtime\myManager\discoveryResources\数据

The "data" directory actually held all the files/directories that I zipped.

“数据”目录实际上保存了我压缩的所有文件/目录。

When I stripped off the path from the data directory, the Results.zip started with "data" instead of "D:". And that file could be opened with Windows 7, 2012, etc.

当我从数据目录中剥离路径时,Results.zip 以“data”而不是“D:”开头。并且该文件可以使用 Windows 7、2012 等打开。

Hope it helps someone in the future.

希望它可以帮助将来的某个人。

Thanks, -Chris

谢谢,-克里斯

回答by Thorbj?rn Ravn Andersen

You are seeing a security feature of Windows protecting you, not indicating the file is incorrect. Most likely because it finds your zip-file to be strange. Can 7zip open the file properly?

您看到的是 Windows 的一项保护您的安全功能,而不是表示文件不正确。很可能是因为它发现您的 zip 文件很奇怪。7zip可以正常打开文件吗?

回答by Roland Illig

Is the Java process that created the file still running? If yes, it may have kept the zip file open, which on Windows usually means that no other process can read from it. Your code should look like:

创建该文件的 Java 进程是否仍在运行?如果是,它可能使 zip 文件保持打开状态,这在 Windows 上通常意味着没有其他进程可以从中读取。您的代码应如下所示:

OutputStream os = new FileOutputStream("reports.zip");
try {
  ZipOutputStream zos = new ZipOutputStream(os);
  ...
} finally {
  os.close();
}

回答by Andrew Thompson

Try the code shown on Problem saving and loading multiple images in a same fileat OTN. Just tested the code again and when I open images.zip by double clicking the file, Windows shows the contents just fine.

尝试在 OTN的同一文件保存和加载多个图像的问题中显示的代码。刚刚再次测试了代码,当我通过双击文件打开 images.zip 时,Windows 显示的内容很好。