• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Unable to create ,zip file

 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
bacon. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic