Error opening zip file created using java

我是研究僧i 提交于 2019-12-04 13:47:10

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.

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.

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:

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

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.

Hope it helps someone in the future.

Thanks, -Chris

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?

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:

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!