普通压缩文件以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'

  

zip压缩文件(二)的更多相关文章

  1. php操作zip压缩文件

    php操作zip压缩文件 一.总结 1.php操作zip:php可以操作zip压缩文件,通过 ZZIPLIB扩展库,这些扩展库可以通过composer安装,或者某些版本的php会自带 2.完美操作zi ...

  2. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  3. java ZIP压缩文件

    问题描述:     使用java ZIP压缩文件和目录 问题解决:     (1)单个文件压缩 注:     以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...

  4. PHP zip压缩文件及解压

    PHP zip压缩文件及解压 利用ZipArchive 类实现 只有有函数.界面大家自己写 ZipArchive(PHP 5.3 + 已自带不需要安装dll) /** * 文件解压 * @param ...

  5. php实现ZIP压缩文件解压缩

    测试使用了两个办法都可以实现: 第一个:需要开启配置php_aip.dll <?php //需开启配置 php_zip.dll //phpinfo(); header("Content ...

  6. java将文件打包成ZIP压缩文件的工具类实例

    package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  7. php实现ZIP压缩文件解压缩(转)

    测试使用了两个办法都可以实现: 第一个:需要开启配置php_aip.dll <?php //需开启配置 php_zip.dll //phpinfo(); header("Content ...

  8. Python 黑客 --- 002 入门级 ZIP压缩文件口令暴力破解机

    Python 黑客 入门级实战:ZIP压缩文件口令暴力破解机 使用的系统:Ubuntu 14.04 LTS Python语言版本:Python 2.7.10 V 编写zip 压缩文件口令破解器需要使用 ...

  9. PHP生成ZIP压缩文件

    PHP生成ZIP压缩文件 /* * 生成zip压缩文件 * $sourceDir:被压缩的文件夹或文件 * $outFileName:输出的压缩文件名称 * */ function createZip ...

  10. ICSharpCode.SharpZipLib.Zip 压缩文件

    public class ZipFileHelper { List<string> urls = new List<string>(); void Director(strin ...

随机推荐

  1. 页面性能分析-Chrome Dev Tools

    一.分析面板介绍 进行页面性能快速分析的主要是图中圈出来的几个模块功能: Network : 页面中各种资源请求的情况,这里能看到资源的名称.状态.使用的协议(http1/http2/quic...) ...

  2. seo 回忆录百度基本概念(一)

    前言 我以前的博客自己做的seo,现在拿来和大家一起交流,是白帽哈,黑帽的不敢发,也不敢学[微笑]. 正文 为什么做seo 做seo说到底就是为了排名.为什么需要排名呢?因为现在人比较懒,只会去查看第 ...

  3. css中的宽和高

    width width表示宽 height height表示高 max-width.min-width max-width表示最大宽度 min-width表示最小宽度 max-height.min-h ...

  4. VRRP概念、工作原理

    VRRP是一种路由容错协议,也可以叫做备份路由协议,可以把一个虚拟路由器的责任动态分配到局域网上的 VRRP 路由器中的一台. 控制虚拟路由器 IP 地址的 VRRP 路由器称为主路由器, 它负责转发 ...

  5. tcp的重传与超时

    TCP协议是一种面向连接的可靠的传输层协议,它保证了数据的可靠传输,对于一些出错,超时丢包等问题TCP设计的超时与重传机制. 其基本原理:在发送一个数据之后,就开启一个定时器,若是在这个时间内没有收到 ...

  6. Debugging Under Unix: gdb Tutorial (https://www.cs.cmu.edu/~gilpin/tutorial/)

    //注释掉 #include <iostream.h> //替换为 #include <iostream> using namespace std; Contents Intr ...

  7. opencv-10-图像滤波-噪声添加与均值滤波-含opencv C++ 代码实现

    开始之前 再说上一篇文章中, 我们想按照噪声产生, 然后将降噪的, 但是限于篇幅, 我就放在这一篇里面了, 说起图像的噪声问题就又回到了我们上一章的内容, 把噪声当作信号处理, 实际上数字图像处理实际 ...

  8. 强行重装IE6

    一句指令解决了郁闷一天的问题: 今天碰到问题如下: 在不知是不是人品问题的情况下(其实基本是优化大师嫌疑最大)发现在第三方引用的软件中不能打开IE了: 具体症状: 在QQ中点击别人的链接,没反应: 在 ...

  9. 使用BottomNavigationView+ViewPager+Fragment的底部导航栏

    2019独角兽企业重金招聘Python工程师标准>>> 使用BottomNavigationView做底部工具栏,使用ViewPager做页面切换,使用Fragment完成每个页面的 ...

  10. angularJS中$http.get( ).success( )报错原因及解决方案

    一.问题描述: 电脑安装的angular1.6.7版本,项目中使用了$http.get( ).success( ),控制台报错: $http.get(...).success is not a fun ...