Meaningless Drivel is fun!
The moose likes Java in General and the fly likes Unable to create ,zip file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unable to create ,zip file " Watch "Unable to create ,zip file " New topic

Unable to create ,zip file

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 761

I have written one function for crating zip file

This function creats a zip file of the bin folder in jboss

how do i create zip file for all the folders in my C:\\test folder which contains few folders and few files

Jigar Naik

Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
You can use for either files or directories.
So you can create the source directory like so:

File sourceDir = new File("C:\\test");

and then get all the subdirectories or files as an array of files:

File[] subdirects = sourceDir.listFiles();

Then you can simply test each subdirectory to see if it is a directory or file.

The best way to manipulate a batch of directories and files is to use a recursive process, as in the method zipFiles shown below.

So you can call zipFiles, passing in sourceDir = new File("C:\\test");

BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream("c:\\");
CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));
byte data[] = new byte[BUFFER];

public static void main(String[] args){
zipFiles(new File("C:\\test");
System.out.println("checksum : " + checksum.getChecksum().getValue());
} catch(Exception e){

private static void zipFiles(File dir){
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
}//end for

System.out.println("Adding: "+files[i]);
FileInputStream fi = new
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
int count;
while((count =, 0,BUFFER)) != -1) {
out.write(data, 0, count);

It is sorta covered in the JavaRanch Style Guide.
subject: Unable to create ,zip file
It's not a secret anymore!