packagecom.test;importjava.io.*;importjava.util.*;importjava.util.zip.ZipOutputStream;importjava.util.zip.ZipEntry;importjava.util.zip.ZipFile;publicclassTestZip {publicTestZip() {
}/*** 压缩文件
*
*@paramsrcfile
* File[] 需要压缩的文件列表
*@paramzipfile
* File 压缩后的文件*/publicstaticvoidZipFiles(java.io.File[] srcfile, java.io.File zipfile) {byte[] buf=newbyte[1024];try{//Create the ZIP fileZipOutputStream out=newZipOutputStream(newFileOutputStream(
zipfile));//Compress the filesfor(inti=0; i
FileInputStream in=newFileInputStream(srcfile[i]);//Add ZIP entry to output stream.out.putNextEntry(newZipEntry(srcfile[i].getName()));//Transfer bytes from the file to the ZIP fileintlen;while((len=in.read(buf))>0) {
out.write(buf,0, len);
}//Complete the entryout.closeEntry();
in.close();
}//Complete the ZIP fileout.close();
System.out.println("压缩完成.");
}catch(IOException e) {
e.printStackTrace();
}
}/*** 解压缩
*
*@paramzipfile
* File 需要解压缩的文件
*@paramdescDir
* String 解压后的目标目录*/publicstaticvoidUnZipFiles(java.io.File zipfile, String descDir) {try{//Open the ZIP fileZipFile zf=newZipFile(zipfile);for(Enumeration entries=zf.entries(); entries.hasMoreElements();) {//Get the entry nameZipEntry entry=((ZipEntry) entries.nextElement());
String zipEntryName=entry.getName();
InputStream in=zf.getInputStream(entry);
OutputStream out=newFileOutputStream(descDir+zipEntryName);byte[] buf1=newbyte[1024];intlen;while((len=in.read(buf1))>0) {
out.write(buf1,0, len);
}//Close the file and streamin.close();
out.close();
System.out.println("解压缩完成.");
}
}catch(IOException e) {
e.printStackTrace();
}
}
}