wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to zip files and folders in a directory into 1 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 zip files and folders in a directory into 1 zip file?" Watch "How to zip files and folders in a directory into 1 zip file?" New topic
Author

How to zip files and folders in a directory into 1 zip file?

Ong CherYee
Greenhorn

Joined: Jan 17, 2008
Posts: 11
I have written a zip code which can zip files from a directory into 1 zip file. However, when it encounters a folder in that directory, it compresses the folder into a seperate zip file.

Below is the code:



The part where the folder is dealt with is under the "elseif fileArray[i].isDirectory()) ". Recursion is used to get into the folder to read the files inside for compression.

I have made the file and zip output streams to have the same location (destZipFile) so that the folder to be compressed can be placed in the same zip file. However, when I ran the program, there is no trace of the folder being zipped.

How do I manage to zip all files and folders inside a directory into 1 zip file?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3601
    
  15
You're creating a new ZipOutputStream every time you call the method. These two linesneed to be taken out and put before the first call to your method. Then pass zipOS to the method instead of destZipFile.
[ January 24, 2008: Message edited by: Joanne Neal ]

Joanne
 
GeeCON Prague 2014
 
subject: How to zip files and folders in a directory into 1 zip file?