This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes java.util.zip : Unzipping gives Windows error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.util.zip : Unzipping gives Windows error" Watch "java.util.zip : Unzipping gives Windows error" New topic
Author

java.util.zip : Unzipping gives Windows error

Swati Sisodia
Greenhorn

Joined: Oct 28, 2007
Posts: 19
Hi all,

I have recently started working in J2EE. (I am a J2ME Developer basically).
I ran into this problem I cannot figure out! I have to zip a folder and make it available for downloading. The folder may have nested folders and files. The code does zips the folder and downloads, but when I try to unzip it via WinZip, it gives me error: "Windows has blocked access to these files to help protect your system". I also tried unblocking it from the zip folder properties, but no use. Surprisingly, the downloaded zip file has the desired file size and I was able to unzip it with 7-zip utility and get all the content in correct folder structure. I want to make it available via WinZip. Can anybody figure out the problem for me please.
Below is my method for zipping (I am pretty sure on passing correct file path name as dir2zip and properly flushing and closing ZipOutputStream zos later):



Lemme know if I need to give more information. Thanks much.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Since 'filePath' is the absolute path to the file you are currently adding to the zip and you are using this full path as the zip entry name I suspect that poor old WinZip is trying to create files with this full path and is running into OS security constraints.

When I do this I use a zip entry name which is relative to the root directory. Easy enough to do by adding another parameter to the method to say how much of the full path to chop off the front of the full path.


then



and



The first time you call this you set the prefixLength to be dir2zip.length().

P.S. You will find you have less code if instead of using a String for 'dir2zip' you use java.io.File and then use



Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Swati Sisodia
Greenhorn

Joined: Oct 28, 2007
Posts: 19
whao... it works! I spent days to figure this out. I love this forum. Thanks James.

Well, now, I have a relatively smaller problem, I am loosing all the internal directory structure when I chop off the root directory name while entering the files into the zip, and all my content gets into the one folder, which is downloaded as a zip file. I want to maintain my directory structure of downloaded zip. I get from API doc that ZipOutputStream considers an entry as a directory if the name ends with "/" character. So, that way I can create nested folders. Now, my question is how do I point a particular file to go into its own directory or sub-directory. (At the time of inserting, I have the information of the directory it belongs to.)

FYI, the server, where the original folders are kept for zipping is a Linux machine. And the client machine can be either Windows or Mac. So, I have to consider that while thinking of root directories and absolute paths.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Swati Sisodia wrote:whao... it works! I spent days to figure this out. I love this forum. Thanks James.

No problem.

Well, now, I have a relatively smaller problem, I am loosing all the internal directory structure when I chop off the root directory name while entering the files into the zip, and all my content gets into the one folder, which is downloaded as a zip file.


You are chopping off too much from the filename! You only need to chop off the length of the root directory and not the length of the directory.


I want to maintain my directory structure of downloaded zip. I get from API doc that ZipOutputStream considers an entry as a directory if the name ends with "/" character. So, that way I can create nested folders. Now, my question is how do I point a particular file to go into its own directory or sub-directory. (At the time of inserting, I have the information of the directory it belongs to.)


Only chop off the length of the root directory then it will happen automatically.


FYI, the server, where the original folders are kept for zipping is a Linux machine. And the client machine can be either Windows or Mac. So, I have to consider that while thinking of root directories and absolute paths.
Swati Sisodia
Greenhorn

Joined: Oct 28, 2007
Posts: 19
Thanks a ton James. You made my day!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.zip : Unzipping gives Windows error
 
Similar Threads
Compress .zip using Java
Java ZipFile
Windows does not recognize Java created ZIP
Tomcat cannot unzip a War file
how to put two different folders(which is in different location) into one zip entry