public class TestZip {
public static String SERPEROT = /;
public static int BUFFER = ;
public static void main(String args[]){
zip(e:/hello/ e:/hellozip);
}
public static void zip(String srcFile String descFile){
ZipOutputStream zos = null;
FileOutputStream fos = null;
File file = null;
try {
fos = new FileOutputStream(descFile);
zos = new ZipOutputStream(fos);
file = new File(srcFile);
String folder = srcFilesubstring(srcFilelastIndexOf(/) +srcFilelength());
zip(zosfilefolder);
} catch (FileNotFoundException e) {
eprintStackTrace();
}finally{
try{
if(zos != null){zosclose();}
if(fos != null){fosclose();}
}catch(Exception e){
eprintStackTrace();
}
}
}
private static void zip(ZipOutputStream descFile File srcFileString srcfolder){
FileInputStream fis = null;
Systemoutprintln(srcFileisDirectory());
try{
if(srcFileisDirectory()){
File[] files = srcFilelistFiles();
descFileputNextEntry(new ZipEntry(srcfolder + /)); //是压缩包里面的路径
srcfolder = srcfolderlength() == ? : srcfolder + /;
Systemoutprintln(srcfolder);
for(int i= ; i<fileslength; i++){
zip(descFilefiles[i]srcfolder + files[i]getName());
}
}else{
descFileputNextEntry(new ZipEntry(srcfolder));
fis = new FileInputStream(srcFile);
byte[] bytes = new byte[];
int n =;
while((n = fisread(bytes)) != ){
descFilewrite(bytesn);
}
}
}catch(Exception e){
eprintStackTrace();
}finally{
try{
if(fis != null){fisclose();}
}catch(Exception e){
eprintStackTrace();
}
}
}
}