最近在一个项目里因涉及到大数据量的压缩,发现通用的递归压缩的效率比较慢,我在项目测试中对一个2G大小的文件进行压缩发现需要4个小时才能压缩完成,感觉效率比较慢,因此查找了一些资料,发现使用ant的zipAPI接口进行压缩可以大大提高压缩效率,并且代码更简洁,初步评估效率可提高15倍,下面是测试样例
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.zip.*;
public class ZipTest {
public void antZip(String src,String dest) {
Zip zip = new Zip();
try {
zip.setBasedir(new File(src));
Project p = new Project();
zip.setDestFile(new File(dest));
p.setBaseDir(new File(src));
zip.setProject(p);
zip.execute();
} catch (Exception e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
public void commZip(String src, String dest) {
try {
FileOutputStream out = new FileOutputStream(new String(dest
.getBytes("gb2312")));
ZipOutputStream zOut = new ZipOutputStream(out);
zip(zOut, new File(src), "");
zOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
private void zip(ZipOutputStream zOut, File file, String base)
throws Exception {
if (file.isDirectory()) {
File[] listFiles = file.listFiles();
zOut.putNextEntry(new ZipEntry(base + "/"));
base = (base.length() == 0 ? "" : base + "/");
for (int i = 0; i < listFiles.length; i++) {
zip(zOut, listFiles[i], base + listFiles[i].getName());
}
} else {
if (base == "") {
base = file.getName();
}
zOut.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(file);
int len;
while ((len = in.read()) != -1) {
zOut.write(len);
}
in.close();
}
}
public static void main(String[] args) {
ZipTest zip=new ZipTest();
//ant zip
long start = System.currentTimeMillis();
zip.antZip("F:\\workspace\\idrs_yingji\\WebRoot\\WEB-INF\\classes",
"F:\\test.zip");
long end = System.currentTimeMillis();
System.out.println("ant zip 耗时::" + (end - start) / 1000);
//common zip
long start1 = System.currentTimeMillis();
zip.commZip("F:\\workspace\\idrs_yingji\\WebRoot\\WEB-INF\\classes",
"F:\\test1.zip");
long end1 = System.currentTimeMillis();
System.out.println("common zip 耗时::" + (end1 - start1) / 1000);
}
}
测试结果
ant zip 耗时::1
common zip 耗时::15
后面将项目的压缩方式改为ANT的接口后,对一个2G的目录进行压缩只用了不到3分钟,而之前需要4.5小时