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 ...
随机推荐
- Centos8安装Docker提示:package docker-ce-3:19.03.8-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed
Centos8安装Docker提示:package docker-ce-3:19.03.8-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but ...
- Jquery中 $.Ajax() 参数详解
1.url:要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type:要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如pu ...
- TensorFlow的模型保存与加载
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf #tensorboard --logdir=&qu ...
- Python爬虫篇(代理IP)--lizaza.cn
在做网络爬虫的过程中经常会遇到请求次数过多无法访问的现象,这种情况下就可以使用代理IP来解决.但是网上的代理IP要么收费,要么没有API接口.秉着能省则省的原则,自己创建一个代理IP库. 废话不多说, ...
- ES6新增的Map和WeakMap 又是什么玩意?非常详细的解释
上一篇文章讲了set和weakSet,这节咱就讲Map和weakMap是什么?这两篇文章并没有什么联系,主要知识用法类似而已.嘿嘿,是不是感觉舒服多了. 什么是Map 介绍什么是Map,就不得不说起O ...
- 2019-2020-1 20199326《Linux内核原理与分析》第一周作业
开篇概述 我利用假期的时间自学了实验楼上的Linux基础入门前八个实验的课程,学习过程中遇到了一些小问题.但经过查资料等方式最终还是解决了问题.现将学到的一些知识点总结下来.方便日后复习查看. 1.零 ...
- 【K8S】基于单Master节点安装K8S集群
写在前面 最近在研究K8S,今天就输出部分研究成果吧,后续也会持续更新. 集群规划 IP 主机名 节点 操作系统版本 192.168.175.101 binghe101 Master CentOS 8 ...
- Clickhouse 时区转换(下)
Clickhouse 时区转换续—时区参数转换 天天加班,时间不够,主要还是我太懒,流汗,,,,,,另外如果这篇学习笔记超过100阅读量并有评论,我可能半夜也会爬起来更新的. 相信大家看我之前记录的这 ...
- HBase Filter 过滤器之RowFilter详解
前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考.RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase ...
- vue添加,删除内容
vue 提交添加内容,点击删除内容 1 html <input v-model="inputValue" /> <button @click="hand ...