aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 3169
    
  10
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to zip files and folders in a directory into 1 zip file?
 
Similar Threads
Problem with searching for a file which is in the zip file
Progress of zipping of folder on ProgressMonitor??
Zipping files
I cant figure out why this zipping code produces an exception :(
Compress .zip using Java