Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.zip : Unzipping gives Windows error

 
Swati Sisodia
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Swati Sisodia
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton James. You made my day!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic