昨天微信公众号上看到了一篇《Java压缩20M文件从30秒到1秒的优化过程》的文章,记录一下,最后作者的感悟,受教了。
可以关注一下
原文章链接我没找到(太懒了),加公众号自己去找吧。
不废话,直接上代码
package com.test;
import java.io.*;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class TestZip {
public static void main(String[] args) {
testZip3();
}
public static void testZip1() {
String targetZipPath = "C:\\Users\\111\\Desktop\\test.zip";
String installFile = "C:\\Users\\111\\Desktop\\npp.7.8.8.Installer.x64.exe";
File zipFile = new File(targetZipPath);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile))) {
//开始时间
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
try (InputStream input = new FileInputStream(installFile)) {
zipOut.putNextEntry(new ZipEntry("test" + i));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
}
}
System.out.println(System.currentTimeMillis() - beginTime);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testZip2() {
String targetZipPath = "C:\\Users\\111\\Desktop\\test.zip";
String installFile = "C:\\Users\\111\\Desktop\\npp.7.8.8.Installer.x64.exe";
File zipFile = new File(targetZipPath);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut)) {
//开始时间
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(installFile))) {
zipOut.putNextEntry(new ZipEntry("test" + i + ".exe"));
int temp = 0;
while ((temp = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(temp);
}
}
}
System.out.println(System.currentTimeMillis() - beginTime);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testZip3() {
String targetZipPath = "C:\\Users\\111\\Desktop\\test.zip";
String installFile = "C:\\Users\\111\\Desktop\\npp.7.8.8.Installer.x64.exe";
File zipFile = new File(targetZipPath);
long intallFileSize = new File(installFile).length();
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
//开始时间
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
try (FileChannel fileChannel = new FileInputStream(installFile).getChannel()) {
zipOut.putNextEntry(new ZipEntry(i + ".exe"));
fileChannel.transferTo(0, intallFileSize, writableByteChannel);
}
}
System.out.println(System.currentTimeMillis() - beginTime);
} catch (Exception e) {
e.printStackTrace();
}
}
}
嗯,没有文章作者那么nb,就简单写一下优化思路
testZip1() 最初版本
效率 :最低
原因:使用InputStream.read方法,此方法按字节读取数据,不停的io,很慢!
testZip2() 优化第一步 改用BufferedInputStream
效率:相对于testZIp1() 提升巨大
原因: BufferedInputStream内部封装了一个byte数组用于存放数据,默认大小是8192,缓存区满了进行一次IO,大大降低IO浪费的时间。
testZip3() 使用NIO策略
效率:在大数据量下,较testZip2() 有很大提升。
原因: 可以理解为异步IO,异步就是速度快!!!(等着有时间复盘一下NIO,东西很多)