先在微信公众号上看到网文《将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. GitLab 后台修改用户密码

    GitLab 后台修改用户密码 # 打开控制台 gitlab-rails console production # 找到用户,输入密码,确认密码,保存 user = User.find_by(user ...

  2. 2020-07-31:给定一个二叉搜索树(BST),找到树中第K 小的节点。

    福哥答案2020-07-31: BST 的中序遍历是升序序列.1.递归法.时间复杂度:O(N),遍历了整个树.空间复杂度:O(N),用了一个数组存储中序序列.2.迭代法.时间复杂度:O(H+k),其中 ...

  3. C#LeetCode刷题之#383-赎金信(Ransom Note)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...

  4. Linux学习日志第一天——基础命令①

    文章目录 前言 命令的作用及基本构成 关于路径 命令 ls (list) 命令 pwd (print working directory) 命令cd (change directory) 命令 mkd ...

  5. LeetCode 861翻转矩阵后得分详细解法

    1. 题目内容 有一个二维矩阵 A 其中每个元素的值为 0 或 1 . 移动是指选择任一行或列,并转换该行或列中的每一个值:将所有 0 都更改为 1,将所有 1 都更改为 0. 在做出任意次数的移动后 ...

  6. 在虚拟机中安装Mysql

    目录 下载Mysql 安装 配置mysql允许远程访问 下载Mysql 下载地址:http://dev.mysql.com/downloads/mysql 我这里下载的是安装版本 安装 配置mysql ...

  7. Docker日常使用方式

    前提 在安装docker之前,建议你设置系统的国内镜像源先哦,很快~嗯,快. 阿里云镜像源:https://developer.aliyun.com/mirror/ 安装 安装docker 下面都是官 ...

  8. cdh6.2.1搭建安装

    1.为CM安装mysql驱动 4台服务器 将mysql-connector-java-5.1.27-bin.jar拷贝到/usr/share/java路径下,并重命名 mv mysql-connect ...

  9. Hive学习目录

    大数据之Hive学习目录 第 1 章 Hive入门 1.1 什么是Hive 1.2 Hive的优缺点 1.2.1 优点 1.2.2 缺点 1.3 *Hive架构原理 1.4 Hive和数据库比较 第 ...

  10. 在 Linux 中查找和删除重复文件

    原文链接:https://www.linuxprobe.com/linux-FSlint.html FSlint同时具有GUI和CLI模式.因此,对于新手来说,这是一个用户友好的工具.FSlint不仅 ...