publicvoidcreateZip(String sourceDir, String zipFileName)throwsException {
File folderObject=newFile(sourceDir);if(folderObject.exists()) {
ArrayListfileList=getSubFiles(newFile(sourceDir));
ZipOutputStream zos=newZipOutputStream(newFileOutputStream(zipFileName));
ZipEntry ze=null;byte[] buf=newbyte[1024];intreadLen=0;for(inti=0; i
File f=(File)fileList.get(i);
System.out.println("Adding:"+f.getPath()+f.getName());
ze=newZipEntry(getAbsFileName(sourceDir, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
zos.putNextEntry(ze);
InputStream is=newBufferedInputStream(newFileInputStream(f));while((readLen=is.read(buf,0,1024))!=-1) {
zos.write(buf,0, readLen);
}
is.close();
System.out.println("done...");
}
zos.close();
}else{thrownewException("The folder is not exist");
}
}privateArrayListgetSubFiles(File baseDir) {
ArrayListret=newArrayList();
File[] tmp=baseDir.listFiles();for(inti=0; i
ret.add(tmp[i]);
}if(tmp[i].isDirectory()) {
ret.addAll(getSubFiles(tmp[i]));
}
}returnret;
}privateString getAbsFileName(String baseDir, File realFileName) {
File real=realFileName;
File base=newFile(baseDir);
String ret=real.getName();while(true) {
real=real.getParentFile();if(real==null)break;if(real.equals(base))break;else{
ret=real.getName()+"/"+ret;
}
}returnret;
}