aspose file tools*
The moose likes Java in General and the fly likes Creation of .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 "Creation of .zip file???" Watch "Creation of .zip file???" New topic
Author

Creation of .zip file???

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,
Iam new to this package.I want to compress one file(for example test.doc)for that I want to make .zip file of that .doc file.Iam selecting file through opening FileDialog on the actionevent of button and now I want to compress that file in .zip format at maximum level of compression "9".Can any one plz give me some code for that.For this help I shall ever gratfull to u.
Thanks in Advance
Bikash :roll:
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Did you check out the classes in java.util.zip, or is that what you're asking about?
--Mark
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Mark,
Ya, Iam asking about java.util.zip package but Iam confused how I can set the compression level.Can plz give me some code for that.
Regards
Bikash
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Well, I've never used it, but the class java.util.zip.Deflater looks like what you want. Just create a Deflator, set the level in the call to the consturctor, put the bytes into it, then get the bytes out.
--Mark
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Mark,
Below r my codes can u tell where I should add Deflater class with specified level I know we can set the level from 0-9 for best compression I want to use level "9".Can u plz help me.Iam trying this coz i want to compress my files during uploading.
import java.io.*;
import java.util.zip.*;
public class zipper1 {
public static final void main(String [] args){
try {
File myDir=File("c:\\html");
ZipOutputStream outStream = new ZipOutputStream (new FileOutputStream("c:\\html\\out.zip"));
outStream.setMethod(ZipOutputStream.DEFLATED);
processFile(myDir, "SPLASH.gif",outStream);
outStream.close();
}
catch(Exception e){
e.printStackTrace();
}
}
static void processFile(File myDir, String fileName, ZipOutputStream out)
{
try
{
File f = new File(myDir, fileName);
FileInputStream in = new FileInputStream(f);
// Add ZIP entry to output stream.
ZipEntry e1 = new ZipEntry(fileName);
out.putNextEntry(e1);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
catch (Exception e)
{
System.out.println("Error ::" + e);
}

}
}
Regards
Bikash
:roll:
 
Consider Paul's rocket mass heater.
 
subject: Creation of .zip file???