File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to write zip files into another zip file..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to write zip files into another zip file..?" Watch "How to write zip files into another zip file..?" New topic
Author

How to write zip files into another zip file..?

Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Hi everybody,

Does any one know how to write zip files into antoher zip file? In my application I have to provide the user to export the files he is intrested into a zip file. There are different category of files, so I need to put each category into a directory, zip it and put all the directories into another zip file.

assuming I have three directories which includes two files each,

directory1
directory2
directory3

Now I have to create three zip files for each of them director1.zip, directory2.zip, direcoty3.zip.

Then I have to create a zip file which contains all three zip files. Lets say "parentdirectory.zip".

How can I do this? I have tried it, but I could write indvidual files into the final directory "parentdirectory,zip". How can I write each "directory.zip" as it is into "parentdirectory,zip". So that my zip file contains the following structure

parentdiretory.zip
|
|_direcoty1.zip
|_directory2.zip
|_directory3.zip

Any ideas...?

One more thing each time I create a individual zip for each directory it was leaving the directory in my file system. As I write this zip into the parentdirectory.zip anyway, I dont want the individual directory to be in my file system.

Thanks,
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2288
    
  49
How have you tried to write the zip files into a new zip? You should be able to write them in exactly the same way you wrote the intial files into the zip.

If you want to get rid of an empty directory (is this what you meant?) create a File object for the directory and call it's delete() method. If the directory contains files you will need to delete them first - you can get a an array of all files using the directories File object's listFiles() method.
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Thanks Tony....

Here is how I did

I have a method to make zip file of individual directory and return as a file



@Henry,

Thanks for the clarification. I will change it..
[ August 21, 2007: Message edited by: Chinna K ]
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2288
    
  49
Now I'm confused, you said you wanted to add the zip files (as they are) to another zip file but the code you've shown appears to be attempting to unzip the zip file and add each individual entry to the new zip file.

Which do you want to do?
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Yes you are right, here I am extracting the files again and writing. This is my qeustion, how to read zip file? I tried reading the zip file just like a normal file it was throwing an expcetion saying that there is no zip entry.

Is there a way to read the zip files?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Chinna K, your name is still not conforming to the JavaRanch naming policy. Your name should consist of a first name, space and a second name. A single letter "K" as a second name is not acceptable.

Please take a moment to carefully read the naming policy and change your display name.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Hello ranchers,

Any ideas...?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You write a zip file into a zip file exactly the same way as you would write a pdf file into a zip file. That's all. Apparently you already know how to put files into a zip file, so just do that.
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Paul,

Thanks for your reply. I dont think that reading a zip file is not the same as reading any other file (say pdf or test). As per the tests I have run when you read a zip file you need to get the individual "ZipEntry" inside the zip file and write them. I dont know which way you are asking me to do that.

Correct me if I am wrong.

Thanks,
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Ravi Kotha:
Paul,

Thanks for your reply. I dont think that reading a zip file is not the same as reading any other file (say pdf or test). As per the tests I have run when you read a zip file you need to get the individual "ZipEntry" inside the zip file and write them. I dont know which way you are asking me to do that.

Correct me if I am wrong.

Thanks,
I think you are wrong. A file is just a series of bytes. If you were putting a pdf file into a zip file you would read its bytes and put them into a ZipEntry. Likewise if you are putting a zip file into a zip file you should read its bytes and put them into a ZipEntry. There's no difference at all. Don't try and make simple things complicated.
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Paul,

Thanks for correcting me. Actually I have tried reading the zip file as a normal file but because of some other bug in my code it didnt work. So, I thought like I have to use "ZipInputStream" to read the zip files. I tried it this time and corrected the other bug. It worked. Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Glad to hear that. I have just spent the last five hours debugging something that turned out to be not a problem at all, so I know how that can happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write zip files into another zip file..?