普通压缩文件以20M大小的文件为例

   public static void main(String[] args) {
String source = "F:\\demo\\择天记 第5季 第01话 标清(270P).qlv";
String zipFile = "F:\\demo\\zip\\择天记.zip";
zipFileNoBuffer(zipFile, source);
} public static void zipFileNoBuffer(String zipFilePath, String sourceFilePath) {
try {
File zipFile = new File(zipFilePath);
File sourceFile = new File(sourceFilePath);
long length = sourceFile.length();
System.out.println("开始压缩文件、文件大小:[" + formetFileSize(length) + "] ...");
long beginTime = System.currentTimeMillis();
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
InputStream input = new FileInputStream(sourceFile);
zipOut.putNextEntry(new ZipEntry(sourceFile.getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
zipOut.closeEntry();
zipOut.close();
//时间耗时
System.out.println("压缩完毕,耗时:[" + printTimeConsuming(beginTime) + "] ms ...");
} catch (Exception e) {
e.printStackTrace();
}
}

结果:

Connected to the target VM, address: '127.0.0.1:49281', transport: 'socket'
开始压缩文件、文件大小:[21.24M] ...
压缩完毕,耗时:[68652] ms ...
Disconnected from the target VM, address: '127.0.0.1:49281', transport: 'socket'

  

利用缓冲区BufferInputStream 优化

   private static void firstZipFile(String zipFilePath, String sourceFilePath){
try {
File zipFile = new File(zipFilePath);
File sourceFile = new File(sourceFilePath);
long length = sourceFile.length();
System.out.println("开始压缩文件、文件大小:[" + formetFileSize(length) + "] ...");
long beginTime = System.currentTimeMillis();
FileOutputStream fileOut = new FileOutputStream(zipFile);
ZipOutputStream zipOut = new ZipOutputStream(fileOut);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut);
InputStream input = new FileInputStream(sourceFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
zipOut.putNextEntry(new ZipEntry(sourceFile.getName()));
int temp = 0;
while ((temp = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(temp);
}
bufferedInputStream.close();
input.close();
bufferedOutputStream.close();
zipOut.close();
fileOut.close();
//时间耗时
System.out.println("压缩完毕,耗时:[" + printTimeConsuming(beginTime) + "] ms ...");
} catch (Exception e) {
e.printStackTrace();
}
}

结果:耗时缩短了(68652-2501)毫秒

Connected to the target VM, address: '127.0.0.1:51524', transport: 'socket'
开始压缩文件、文件大小:[21.24M] ...
压缩完毕,耗时:[2501] ms ...
Disconnected from the target VM, address: '127.0.0.1:51524', transport: 'socket'

 说明:

这是一个调用本地方法与原生操作系统进行交互,从磁盘中读取数据。每读取一个字节的数据就调用一次本地方法与操作系统交互,是非常耗时的。例如我们现在有30000个字节的数据,如果使用FileInputStream那么就需要调用30000次的本地方法来获取这些数据,而如果使用缓冲区的话(这里假设初始的缓冲区大小足够放下30000字节的数据)那么只需要调用一次就行。因为缓冲区在第一次调用read()方法的时候会直接从磁盘中将数据直接读取到内存中。随后再一个字节一个字节的慢慢返回。

第二次优化:

 /**
* 第二次优化使用 Channel
*/
private static void secondZipFile(String zipFilePath, String sourceFilePath ){
try {
File zipFile = new File(zipFilePath);
File sourceFile = new File(sourceFilePath);
long length = sourceFile.length();
System.out.println("开始压缩文件、文件大小:[" + formetFileSize(length) + "] ...");
long beginTime = System.currentTimeMillis();
FileOutputStream fileOut = new FileOutputStream(zipFile);
ZipOutputStream zipOut = new ZipOutputStream(fileOut);
WritableByteChannel writableByteChannel = Channels.newChannel(zipOut);
FileInputStream input = new FileInputStream(sourceFile);
FileChannel channel = input.getChannel();
zipOut.putNextEntry(new ZipEntry(sourceFile.getName()));
channel.transferTo(0, length, writableByteChannel);
channel.close();
input.close();
writableByteChannel.close();
zipOut.close();
fileOut.close();
//时间耗时
System.out.println("压缩完毕,耗时:[" + printTimeConsuming(beginTime) + "] ms ...");
} catch (Exception e) {
e.printStackTrace();
}
}

结果:比第一次优化耗时缩短了(2501-1810)毫秒

Connected to the target VM, address: '127.0.0.1:52021', transport: 'socket'
开始压缩文件、文件大小:[21.24M] ...
压缩完毕,耗时:[1810] ms ...
Disconnected from the target VM, address: '127.0.0.1:52021', transport: 'socket'

  

最新文章

  1. mysql 7下载安装及问题解决
  2. PHP设计模式(五)建造者模式(Builder For PHP)
  3. SQL Server 即时文件初始化
  4. EC笔记,第二部分:8.别让异常逃离析构函数
  5. Spring Setter Injection and Constructor Injection
  6. centos7 静态ip设置
  7. centos6.4 64位下安装nfs文件共享系统
  8. 集群(cluster)原理(转)
  9. 移动端1px细线的处理
  10. NaN 和 Infinity
  11. HTML5每日一练之input新增加的URL类型与email类型应用
  12. A Tour of Go Mutating Maps
  13. Tomcat启动报错 Failed to start component [StandardServer[8005]]解决
  14. MySql启动提示:The server quit without updating PID file(…)失败
  15. WinXP 无线技巧“区域没有通过无线网络中的发现”一个可能的原因!
  16. iOS基础 - Quartz 2D绘图的基本步骤
  17. [Usaco2008 Mar]River Crossing渡河问题[简单DP]
  18. mui开发app之多图压缩与上传(仿qq空间说说发表)
  19. 学习js的点点滴滴记录
  20. Js的闭包,这篇写的是比较清晰明了的

热门文章

  1. 2019-2020-1 20199328《Linux内核原理与分析》第二周作业
  2. AtomineerUtils使用说明
  3. 使用Node.js的http-serve搭建本地服务器
  4. RHCS图形界面建立GFS共享下
  5. 07 模型层 orm相关查询 F查询Q查询 django开启事务
  6. java学习first_day
  7. celery的定时任务
  8. 推荐一款Python数据可视化神器
  9. Edge Weight Assignment(树-异或-贪心)
  10. G - Pictures with Kittens (easy version) dp