Hint: Java.util.zipoutputstream
Java API compressed to zip file
Code:
Copy Code code as follows:
Package com.gaoqi.test;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipOutputStream;
/**
* Compress files
* @author Administrator
*
*/
public class Simplezip {
public static final int buffer_size = 1024;
public static void Main (string[] args) throws IOException {
String src = "d:\\chat";
String des = "d:\\chat01.zip";
Zipoutputstream zos = null;
try{
Zos = new Zipoutputstream (new FileOutputStream (DES));
File Srcfile = new file (src);
String base = Srcfile.getname ();
Filezip (srcfile,zos,base);
}catch (Exception e) {
//Todo:handle Exception
E.printstacktrace ();
} finally{
if (zos!=null) {
Zos.close ();
}
}
System.out.println ("file compression succeeded" + src);
}
private static void Filezip (File srcfile, Zipoutputstream Zos, String Base)
Throws exception{
TODO auto-generated Method Stub
if (!srcfile.exists ()) {
SYSTEM.OUT.PRINTLN ("File does not exist" + srcfile.getpath ());
}
if (Srcfile.isfile ()) {
Zos.putnextentry (new ZipEntry (base));
FileInputStream fis = new FileInputStream (srcfile);
byte[] buf = new Byte[buffer_size];
int n=0;
while ((N=fis.read (buf, 0, buf.length))!=-1) {
Zos.write (buf, 0, N);
}
Fis.close ();
}else{
if (Srcfile.isdirectory ()) {
Base = base + File.separator;
file[] Subfiles = Srcfile.listfiles ();
for (File subfile:subfiles) {
Filezip (Subfile, Zos, base + subfile.getname ());
}
}
}
}
}