如图,10000多次/秒,CPU占用也高,压缩10M的文件要1分多钟,非常慢。我的思路是能不能在压缩时,在代码中加入缓冲区,待缓冲区满了再写文件?如果可以加缓冲区,代码要怎么写呢,下面...
如图,10000多次/秒,CPU 占用也高,压缩10M的文件要1分多钟,非常慢。
我的思路是能不能在压缩时,在代码中加入缓冲区,待缓冲区满了再写文件?
如果可以加缓冲区,代码要怎么写呢,下面贴上全部代码,请各位帮忙解决一下:
/***********************************************************************************************/
public class Zip {
public void zip(String inputFileName, String zipFileName) throws Exception {
//System.out.println(zipFileName);//输出正在压缩的文件名
zip(zipFileName, new File(inputFileName));
}
private void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
zip(out, inputFile, "");
System.out.println("文件夹压缩完成");
out.close();
}
private void zip (ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {//判断是否为目录
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {//压缩目录中的所有文件
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
}
展开