• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ravi Kotha
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers,

Any ideas...?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic