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 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unable to create ,zip file " Watch "Unable to create ,zip file " New topic
Author

Unable to create ,zip file

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
Hi

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 java.io.File 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:\\MyZip1.zip");
CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];

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


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

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

Julia
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to create ,zip file
 
Similar Threads
Corrupt file name after compression.
Please help me out with zipping :(
problem in attached file .
Problem zipping the file
Not able to put some files inside Zip file