先在微信公众号上看到网文《将20M文件从30秒压缩到1秒,我是如何做到的?》,然后在网上搜索了一下,看到了原文:https://www.jianshu.com/p/2e46ccb125ef

很惊奇他把时间压缩到了三十分之一,于是就有了做实验验证的想法。

我的实验对象是apache-tomcat-9.0.30.zip,Redis-x64-3.2.100.msi,Redis-x64-3.2.100.zip这三个文件,加起来21M,比作者的十张2M图片略多。

任务是把它们三个压缩成一个result.zip文件。

首先我实验的是rugularZip方法,也是作者提到的第一种方法:

// time elapsed:1s652ms
private boolean rugularZip(String[] fromFiles,String toFile) {
File zipFile=new File(toFile);
byte[] buffer=new byte[BUFFER_SIZE];
int readLen=0; try {
ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile)) ; for(String file:fromFiles) {
File fileWillZip=new File(file); if(fileWillZip.exists()) {
InputStream inputStream=new BufferedInputStream(new FileInputStream(fileWillZip));
String entryName=fileWillZip.getName();// entryName should be a valid filename,no path seperater allowed
zipOut.putNextEntry(new ZipEntry(entryName)); while((readLen=inputStream.read(buffer,0,BUFFER_SIZE))!=-1) {
zipOut.write(buffer,0,readLen);
}
inputStream.close(); }
} zipOut.close();
}catch(Exception e) {
e.printStackTrace();
return false;
} return true;
}

用时1秒652毫秒。

接下来实验的是bufferOuputZip方法,也就是作者提到的第二种方法:

// time elapsed:1s207ms
private boolean bufferOuputZip(String[] fromFiles,String toFile) {
File zipFile=new File(toFile);
byte[] buffer=new byte[BUFFER_SIZE];
int readLen=0; try {
ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile)) ;
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut) ; for(String file:fromFiles) {
File fileWillZip=new File(file); if(fileWillZip.exists()) {
InputStream inputStream=new BufferedInputStream(new FileInputStream(fileWillZip));
String entryName=fileWillZip.getName();// entryName should be a valid filename,no path seperater allowed
zipOut.putNextEntry(new ZipEntry(entryName)); while((readLen=inputStream.read(buffer,0,BUFFER_SIZE))!=-1) {
bufferedOutputStream.write(buffer,0,readLen);
}
inputStream.close();
}
} bufferedOutputStream.flush();
zipOut.close(); return true;
}catch(Exception e) {
e.printStackTrace();
return false;
}
}

用时1秒207毫秒,少了四分之一。

接下来实验作者提出的第三种方法:

// elapsed:1s188ms
private boolean nioChannalZip(String[] fromFiles,String toFile) {
File zipFile=new File(toFile); try {
ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile)) ;
WritableByteChannel wChannel=Channels.newChannel(zipOut); for(String file:fromFiles) {
File fileWillZip=new File(file); if(fileWillZip.exists()) {
FileChannel readChannel=new FileInputStream(fileWillZip).getChannel(); String entryName=fileWillZip.getName();// entryName should be a valid filename,no path seperater allowed
zipOut.putNextEntry(new ZipEntry(entryName)); readChannel.transferTo(0, readChannel.size(),wChannel) ;
readChannel.close();
}
} wChannel.close();
zipOut.close(); return true;
}catch(Exception e) {
e.printStackTrace();
return false;
}
}

一秒188毫秒,没少多少。

接下来再看作者又提到了内存映射文件,他又说和第三种方法速度差不多哦,算了我就不试了。

作者最后又提到pip,我一看原来是用线程,这当然快了,因为主线程只要调用压缩线程就可以返回了,压缩线程消耗的时间不计算在主线程运行时间内,运行当然是秒回。

当然pip里面有个阻塞回调的过程,这消耗了一些时间。

更新DB,写文件都可以另起线程运行,这也是以空间换时间的方法之一。

完整程序:

package zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; // Used to zip a file
public class FileZipper {
private static final int BUFFER_SIZE = 1024; public boolean compressFilesToZip(String[] files,String zipfile) {
return threadZip(files,zipfile);
} private boolean threadZip(String[] fromFiles,String toFile) {
new ZipThread(fromFiles,toFile).start();;
return true;
} // elapsed:1s188ms
private boolean nioChannalZip(String[] fromFiles,String toFile) {
File zipFile=new File(toFile); try {
ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile)) ;
WritableByteChannel wChannel=Channels.newChannel(zipOut); for(String file:fromFiles) {
File fileWillZip=new File(file); if(fileWillZip.exists()) {
FileChannel readChannel=new FileInputStream(fileWillZip).getChannel(); String entryName=fileWillZip.getName();// entryName should be a valid filename,no path seperater allowed
zipOut.putNextEntry(new ZipEntry(entryName)); readChannel.transferTo(0, readChannel.size(),wChannel) ;
readChannel.close();
}
} wChannel.close();
zipOut.close(); return true;
}catch(Exception e) {
e.printStackTrace();
return false;
}
} // time elapsed:1s207ms
private boolean bufferOuputZip(String[] fromFiles,String toFile) {
File zipFile=new File(toFile);
byte[] buffer=new byte[BUFFER_SIZE];
int readLen=0; try {
ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile)) ;
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut) ; for(String file:fromFiles) {
File fileWillZip=new File(file); if(fileWillZip.exists()) {
InputStream inputStream=new BufferedInputStream(new FileInputStream(fileWillZip));
String entryName=fileWillZip.getName();// entryName should be a valid filename,no path seperater allowed
zipOut.putNextEntry(new ZipEntry(entryName)); while((readLen=inputStream.read(buffer,0,BUFFER_SIZE))!=-1) {
bufferedOutputStream.write(buffer,0,readLen);
}
inputStream.close();
}
} bufferedOutputStream.flush();
zipOut.close(); return true;
}catch(Exception e) {
e.printStackTrace();
return false;
}
} // time elapsed:1s652ms
private boolean rugularZip(String[] fromFiles,String toFile) {
File zipFile=new File(toFile);
byte[] buffer=new byte[BUFFER_SIZE];
int readLen=0; try {
ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile)) ; for(String file:fromFiles) {
File fileWillZip=new File(file); if(fileWillZip.exists()) {
InputStream inputStream=new BufferedInputStream(new FileInputStream(fileWillZip));
String entryName=fileWillZip.getName();// entryName should be a valid filename,no path seperater allowed
zipOut.putNextEntry(new ZipEntry(entryName)); while((readLen=inputStream.read(buffer,0,BUFFER_SIZE))!=-1) {
zipOut.write(buffer,0,readLen);
}
inputStream.close(); }
} zipOut.close();
}catch(Exception e) {
e.printStackTrace();
return false;
} return true;
} /**
* change seconds to DayHourMinuteSecond format
*
* @param startMs
* @param endMs
* @return
*/
private static String ms2DHMS(long startMs, long endMs) {
String retval = null;
long secondCount = (endMs - startMs) / 1000;
String ms = (endMs - startMs) % 1000 + "ms"; long days = secondCount / (60 * 60 * 24);
long hours = (secondCount % (60 * 60 * 24)) / (60 * 60);
long minutes = (secondCount % (60 * 60)) / 60;
long seconds = secondCount % 60; if (days > 0) {
retval = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
} else if (hours > 0) {
retval = hours + "h" + minutes + "m" + seconds + "s";
} else if (minutes > 0) {
retval = minutes + "m" + seconds + "s";
} else if(seconds > 0) {
retval = seconds + "s";
}else {
return ms;
} return retval + ms;
} public static String calculateElaspedTime(long startMs) {
long endMs = System.currentTimeMillis();
return ms2DHMS(startMs,endMs);
} public static void main(String[] args) {
String[] files= {"D:\\usr\\apache-tomcat-9.0.30.zip",
"D:\\usr\\Redis-x64-3.2.100.msi",
"D:\\usr\\Redis-x64-3.2.100.zip"};
String zipfile="D:\\usr\\result.zip"; long startMs = System.currentTimeMillis();
FileZipper fz=new FileZipper();
boolean isCreated=fz.compressFilesToZip(files, zipfile);
if(isCreated) {
System.out.println("File:'"+zipfile+"' created,time elapsed:"+calculateElaspedTime(startMs));
}
}
}
package zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; public class ZipThread extends Thread{
private String[] files;
private String zipfile; public ZipThread(String[] files,String zipfile) {
this.files=files;
this.zipfile=zipfile;
} public void run() {
nioChannalZip(this.files,this.zipfile);
} private boolean nioChannalZip(String[] fromFiles,String toFile) {
File zipFile=new File(toFile); try {
ZipOutputStream zipOut=new ZipOutputStream(new FileOutputStream(zipFile)) ;
WritableByteChannel wChannel=Channels.newChannel(zipOut); for(String file:fromFiles) {
File fileWillZip=new File(file); if(fileWillZip.exists()) {
FileChannel readChannel=new FileInputStream(fileWillZip).getChannel(); String entryName=fileWillZip.getName();// entryName should be a valid filename,no path seperater allowed
zipOut.putNextEntry(new ZipEntry(entryName)); readChannel.transferTo(0, readChannel.size(),wChannel) ;
readChannel.close();
}
} wChannel.close();
zipOut.close(); return true;
}catch(Exception e) {
e.printStackTrace();
return false;
}
}
}

--END-- 2020-01-06 14:54

读网文《将20M文件从30秒压缩到1秒,我是如何做到的?》做实验的更多相关文章

  1. 压缩20M文件从30秒到1秒的优化过程

    文章来源公众号:IT牧场 有一个需求需要将前端传过来的10张照片,然后后端进行处理以后压缩成一个压缩包通过网络流传输出去.之前没有接触过用Java压缩文件的,所以就直接上网找了一个例子改了一下用了,改 ...

  2. Pandas 基础(4) - 读/写 Excel 和 CSV 文件

    这一节将分别介绍读/写 Excel 和 CSV 文件的各种方式: - 读入 CSV 文件 首先是准备一个 csv 文件, 这里我用的是 stock_data.csv, 文件我已上传, 大家可以直接下载 ...

  3. Unity shader 官网文档全方位学习(一)

    转载:https://my.oschina.net/u/138823/blog/181131 摘要: 这篇文章主要介绍Surface Shaders基础及Examples详尽解析 What?? Sha ...

  4. 【VR】Leap Motion 官网文档 FingerModel (手指模型)

    前言: 感谢关注和支持这个Leap Motion系列翻译的朋友们,非常抱歉因为工作原因非常久没有更新,今后这个翻译还会继续(除非官方直接给出中文文档).本篇献给大家的是 <FingerModel ...

  5. Spring Security 官网文档学习

    文章目录 通过`maven`向普通的`WEB`项目中引入`spring security` 配置 `spring security` `configure(HttpSecurity)` 方法 自定义U ...

  6. Atitit 基于图片图像 与文档混合文件夹的分类

    Atitit 基于图片图像 与文档混合文件夹的分类 太小的文档(txt doc csv exl ppt pptx)单独分类 Mov10KminiDoc 但是可能会有一些书法图片迁移,因为他们很微小,需 ...

  7. php 读取文件头判断文件类型的实现代码

    php代码实现读取文件头判断文件类型,支持图片.rar.exe等后缀. 例子: <?php $filename = "11.jpg"; //为图片的路径可以用d:/uploa ...

  8. php通过文件头检测文件类型通用类(zip,rar…)(转)

    在做web应用时候,通过web扩展名判断上存文件类型,这个是我们常使用的.有时候我们这样做还不完善.可能有些人上存一些文件,但是他通过修改 扩展名,让在我们的文件类型之内. 单实际访问时候又不能展示( ...

  9. 部署openstack的官网文档解读mysql的配置文件

    部署openstack的官网文档解读mysql的配置文件(使用与ubutu和centos7等系统) author:headsen chen  2017-10-12 16:57:11 个人原创,严禁转载 ...

随机推荐

  1. Docker初探之运行RabbitMQ消息队列服务

    我们平时在使用RabbitMQ是基于Windows操作系统的,在使用前需要安装Er-Lang和RabbitMQ服务程序,如果版本不对RabbitMQ就启动失败,安装流程也比较麻烦. 但如果在Docke ...

  2. Java中的不可变集合,我们换个方式理解!!!

    不可变集合例: public static final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of( "red" ...

  3. Escalate_my_privilege 靶机

    1:扫描主机ip 2:扫描端口发现 22 80 111 3:目录扫描,发现一些平常的页面 4:进入robots.txt发现一个类型命令行的界面,查看是个低权限,但是在home目录下的armour发现密 ...

  4. Linux权限之/etc/passwd文件

    在Linux /etc/passwd文件中每个用户都有一个对应的记录行,它记录了这个用户的一些基本属性.系统管理员经常会接触到这个文件的修改以完成对用户的管理工作.这个文件对所有用户都是可读的.但是L ...

  5. Docker学习笔记-Dockerfile文件详解

    什么是Dockerfile? Docker中有个非常重要的概念叫做--镜像(Image).Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序.库.资源.配置等文件外,还包含了一些为运 ...

  6. python实例 三国人物出场次序 jieba库

    #Cal3kingdoms.py import jieba txt = open("threekingdoms.txt", "r", encoding=&quo ...

  7. JS 替换日期的横杠为斜杠

    例如1: <script type="text/javascript">      var dt = "2010-01-05";           ...

  8. [apue] 一图读懂 unix 文件句柄及文件共享过程

    与文件相关的一些概念 在开始上图之前,先说明几个和 unix 文件密切相关的术语,方便后续讨论使用 文件句柄 / 文件描述符 (file descriptor 或 FD):描述一个打开文件相关属性的类 ...

  9. SpringBoot中加载XML配置

    开篇 在SpringBoot中我们通常都是基于注解来开发的,实话说其实这个功能比较鸡肋,但是,SpringBoot中还是能做到的.所以用不用是一回事,会不会又是另外一回事. 涛锅锅在个人能力能掌握的范 ...

  10. PyCharm 中文教程 01:运行 Python 的四种方式

    <PyCharm 中文指南>在线阅读: http://pycharm.iswbm.com/ Github 项目主页: https://github.com/iswbm/pych... 1. ...