Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creation of .zip file???

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check out the classes in java.util.zip, or is that what you're asking about?
--Mark
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic