zip压缩文件(二)
普通压缩文件以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压缩文件(二)的更多相关文章
- php操作zip压缩文件
php操作zip压缩文件 一.总结 1.php操作zip:php可以操作zip压缩文件,通过 ZZIPLIB扩展库,这些扩展库可以通过composer安装,或者某些版本的php会自带 2.完美操作zi ...
- 【VC++技术杂谈008】使用zlib解压zip压缩文件
最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...
- java ZIP压缩文件
问题描述: 使用java ZIP压缩文件和目录 问题解决: (1)单个文件压缩 注: 以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...
- PHP zip压缩文件及解压
PHP zip压缩文件及解压 利用ZipArchive 类实现 只有有函数.界面大家自己写 ZipArchive(PHP 5.3 + 已自带不需要安装dll) /** * 文件解压 * @param ...
- php实现ZIP压缩文件解压缩
测试使用了两个办法都可以实现: 第一个:需要开启配置php_aip.dll <?php //需开启配置 php_zip.dll //phpinfo(); header("Content ...
- java将文件打包成ZIP压缩文件的工具类实例
package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- php实现ZIP压缩文件解压缩(转)
测试使用了两个办法都可以实现: 第一个:需要开启配置php_aip.dll <?php //需开启配置 php_zip.dll //phpinfo(); header("Content ...
- Python 黑客 --- 002 入门级 ZIP压缩文件口令暴力破解机
Python 黑客 入门级实战:ZIP压缩文件口令暴力破解机 使用的系统:Ubuntu 14.04 LTS Python语言版本:Python 2.7.10 V 编写zip 压缩文件口令破解器需要使用 ...
- PHP生成ZIP压缩文件
PHP生成ZIP压缩文件 /* * 生成zip压缩文件 * $sourceDir:被压缩的文件夹或文件 * $outFileName:输出的压缩文件名称 * */ function createZip ...
- ICSharpCode.SharpZipLib.Zip 压缩文件
public class ZipFileHelper { List<string> urls = new List<string>(); void Director(strin ...
随机推荐
- Java中基础类基础方法(学生类)(手机类)
学生类: //这是我的学生类class Student { //定义变量 //姓名 String name; //null //年龄 int age; //0 //地址 String address; ...
- 理解java容器底层原理--手动实现LinkedList
Node java 中的 LIinkedList 的数据结构是链表,而链表中每一个元素是节点. 我们先定义一下节点: package com.xzlf.collection; public class ...
- JasperReports入门教程(二):中文打印
JasperReports入门教程(二):中文打印 背景 在上一篇中我们介绍了JasperReport的基本入门,也展示了一个报表.但是我们的示例都是使用的英文,如果我们把需要打印的数据改为中文会怎么 ...
- php的一个有意思的命令:-S
php -S localhost:8188 /web 会启动一个监控IP:PORT 的http服务,算是简易的web服务器吧.基本上,实现了PHP+MySQL就可以建立一个简易测试网站的环境.
- input type file onchange上传文件的过程中,同一个文件二次上传无效的问题。
不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为 ...
- LVS+Keepalived 实现高可用负载均衡集群
LVS+Keepalived 实现高可用负载均衡集群 随着网站业务量的增长,网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如 F5 ,Array又太贵,你们又是创业型互联公司如何有效 ...
- Java ArrayList工作原理及实现
http://yikun.github.io/2015/04/04/Java-ArrayList%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE% ...
- vue+elementUI实现权限的部门管理
回头看写过的项目,发现以前感觉有难度的地方,现在想想很简单,在此记录一下,不对的地方欢迎吐槽!!! 复制代码 1.实现效果 2.需求分析 主要用于平台各个部门不同权限的操作,将指定的账号放到对应的权限 ...
- struts2验证码
验证码大多是jsp,servlet写的. 我拿来主义了, 再自己完善了一下(我一直努力想要站在巨人的肩膀) 首先是页面 test.jsp <%@ page contentType=" ...
- 信息竞赛进阶指南--递归法求中缀表达式的值,O(n^2)(模板)
// 递归法求中缀表达式的值,O(n^2) int calc(int l, int r) { // 寻找未被任何括号包含的最后一个加减号 for (int i = r, j = 0; i >= ...