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 ...
随机推荐
- 支付宝开源非侵入式 Android 自动化测试工具 Soloπ
Soloπ(SoloPi)是支付宝开源的一个无线化.非侵入式的Android自动化测试工具,公测版拥有录制回放.性能测试.一机多控三项主要功能,能为测试开发人员节省宝贵时间. 本文是SoloPi团队关 ...
- 10w+QPS 的 Redis 真的只是因为单线程和内存?360° 深入底层设计为你揭开 Redis 神秘面纱!
原文链接:10w+QPS 的 Redis 真的只是因为单线程和内存?360° 深入底层设计为你揭开 Redis 神秘面纱! 你以为 Redis 这么快仅仅因为单线程和基于内存? 那么你想得太少了,我个 ...
- 微信小程序入门(持续更新)
微信小程序的主要文件介绍: . js:脚本文件 .json:配置文件 .wxss:样式表文件 .wxml:页面 微信小程序差不多也是和mvc模式差不多的,采用数据和页面分离的模式,在js上写的数据可以 ...
- session开启慢的原因及解决办法
做微信开发的时候发现微信回复特别慢,发个消息要好几秒才回复,发现不正常后就赶紧找答案,到最后发现是session_start()开启很慢,这是因为session缓存文件过多,默认缓存文件在:win:w ...
- Windows VHD Create, Attach, 获得Disk序号
// create_vhd.cpp : Defines the entry point for the console application. // #include "stdafx.h& ...
- REDHAT7进入单用户模式
Redhat7采用的是grub2,和Redhat6.x进入单用户的方法不同. 一.init方法 1.centos7的grub2界面会有两个入口,正常系统入口和救援模式: 2.修改grub2引导 在正常 ...
- Spring Boot中Spring data注解的使用
文章目录 Spring Data Annotations @Transactional @NoRepositoryBean @Param @Id @Transient @CreatedBy, @Las ...
- Linux网络服务第六章PXE高效能批量网络装机
1.IP地址配置 2.关闭防火墙以及selinux状态如下 systemctl stop firewalld Iptables -F Setenforce 0 三.部署FTP服务 1.安装F ...
- 几年前的今天,Google发了这几篇“大”新闻
免责声明: 因阅读本文所导致的任何时间或经济上的损失,皆由您自行承担,本小编概不负责. 估计今天我的朋友圈会被"震惊!"刷屏,来看看 Google 做过哪些令人"震惊&q ...
- CompressIt
结构 压缩软件的核心在于压缩算法.基于Huffman编码的压缩算法思路: 以二进制方式读取源文件,按照每8bits作为一个字符: 统计每个字符的出现频率即为叶子结点的权值,按照Huffman算法得到每 ...