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 z开发者_StackOverflowipping 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.
regards, Anoop
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
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.
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.
精彩评论