4

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.

regards, Anoop

Anoop
  • 1,439
  • 5
  • 20
  • 37

5 Answers5

6

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

Chris
  • 61
  • 1
  • 1
5

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.

Anoop
  • 1,439
  • 5
  • 20
  • 37
2

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?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • 1
    thnx man, but am not having 7zip in my workstation. tried with winzip and its also failing. the interesting this is that, i can unzip using java itself. i donno whether windows is treating the fils like not trustworthy. – Anoop Nov 01 '10 at 06:59
1

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();
}
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • Hi Roland, thnx 4 the reply. I am closing the outputstream as well as zipoutputstream and the zipentry also. :( – Anoop Nov 01 '10 at 06:57
1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433