java 使用gzip压缩和解压 传输文件必备
java gzip 压缩解压工具类,开箱即用
gzip原理看我另外一篇介绍
压缩效果直接看图:
package com.yeahmobi.datacheck.util;import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;public class CompressUtil {public static void main(String[] args) throws IOException {// 原文件String inFileName = "D:\\response.xls";compressFile(inFileName);// 压缩文件//String gzFileName = "D:\\response.xls.gz";//doUncompressFile(gzFileName);}// 压缩public static void compressFile(String inFileName) throws IOException {FileInputStream in = null;GZIPOutputStream out = null;String outFileName = inFileName + ".gz";try {in = new FileInputStream(new File(inFileName));} catch (FileNotFoundException e) {throw new RuntimeException("文件不存在:" + inFileName);}out = new GZIPOutputStream(new FileOutputStream(outFileName));byte[] buf = new byte[10240];int len = 0;if (in != null && out != null) {try {while (((in.available() > 10240) && (in.read(buf)) > 0)) {out.write(buf);}len = in.available();in.read(buf, 0, len);out.write(buf, 0, len);in.close();out.flush();out.close();} catch (IOException e) {throw new RuntimeException("压缩失败:" + inFileName);}}in.close();out.close();}// 解压public static void doUncompressFile(String inFileName) {try {if (!getExtension(inFileName).equalsIgnoreCase("gz")) {throw new RuntimeException("文件名必须是gz后缀");}GZIPInputStream in = null;try {in = new GZIPInputStream(new FileInputStream(inFileName));} catch (FileNotFoundException e) {throw new RuntimeException("文件不存在 " + inFileName);}String outFileName = getFileName(inFileName);try (FileOutputStream out = new FileOutputStream(outFileName);) {byte[] buf = new byte[1024];int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}in.close();out.close();} catch (FileNotFoundException e) {throw new RuntimeException("解压失败" + outFileName);}} catch (IOException e) {throw new RuntimeException("失败");}}// 获取文件后缀名public static String getExtension(String f) {String ext = "";int i = f.lastIndexOf('.');if (i > 0 && i < f.length() - 1) {ext = f.substring(i + 1);}return ext;}// 获取文件名public static String getFileName(String f) {String fname = "";int i = f.lastIndexOf('.');if (i > 0 && i < f.length() - 1) {fname = f.substring(0, i);}return fname;}
}
java 使用gzip压缩和解压 传输文件必备相关推荐
- Mac下压缩和解压rar文件的方法
命令行 使用工具rarosx,下载地址 选择系统和版本,本文下载的是rarosx-5.4.0.tar.gz 解压缩:tar zxvf rarosx-5.4.0.tar.gz 其中 tar 是Mac 系 ...
- linux fastq.gz文件如何解压,科学网—批量gzip,ungzip快速压缩和解压fastq文件 - 陈明杰的博文...
二代测序fastq文件太大,解压,压缩很浪费时间. 常规:使用gzip gunzip单线程压缩 进阶1:python调用threading,多线程压缩,相当于开了N个窗口进行解压和压缩,略. 进阶2: ...
- Linux gzip压缩/解压 *.gz文件详解
gzip 是linux中常见的压缩/解压工具,最常见的使用对象是*.gz格式的文件,这里简单介绍下它最常见的用法, GZIP(1) General Commands Manual GZIP(1) NA ...
- C#利用自带的ZipFile压缩和解压zip文件,超级简单
ZipFile类是.NET Framework 4.5发布的一个C#压缩文件和解压zip文件的一个类库,它支持.NET Framework 4.5以上版本,包括.NET Core1.0和.NET5以上 ...
- tar和gzip压缩和解压
打包和压缩的区别: 打包:将多文件 封装在一起 压缩:将多文件 封装在一起 通过特定的算法 将冗余的数据 进行删除 tar默认是打包命令,如果想用tar进行压缩 必须加选项 1.gzip格式 压缩: ...
- java GZIP压缩和解压
最近碰到了一个按GZIP解压指定的输入流数据,备份下 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream ...
- java 压缩和解压lzo文件
1.依赖 <dependency><groupId>org.anarres.lzo</groupId><artifactId>lzo-core</ ...
- JavaDemo——使用java.util.zip压缩和解压
Demo: /*** 2019年6月20日下午4:59:37*/ package testzip;import java.io.File; import java.io.FileInputStream ...
- linux 压缩文件夹bz,Linux下tar、bz、gz等压缩包的压缩和解压
Linux下用户经常需要备份计算机系统中的数据,为了节省存储空间,常常将备份文件进行压缩存储,一般可以节约50-90%的存储空间,本文是对压缩和解压命令的大致总结 .tar.gz 或 .tgz 解压: ...
最新文章
- leetcode--在排序数组中查找元素的第一个和最后一个位置--python
- 'utf-8' codec can't decode byte 0xd5 in position 0: invalid continuation byte
- ajax 三种数据格式
- 屠龙之路_战胜View对DataBase猛烈进攻_ThirdDay
- 微电网日前优化调度 。算例有代码(3)
- code vs 集成tfs_关于编译器和集成开发环境,一文给你讲明白!
- LeetCode 题 - 53. 最大子序和 python解法
- java token生成和验证_程序员应该如何设计更优雅的Token认证方式?
- 这N张模板,做出了秒杀echarts和python的可视化大屏,老板很满意
- PHP语言的RSA算法加解密程序
- RDD与DataFrame
- 《大数据之路-阿里巴巴大数据实践》拆书稿以及数仓架构的思考
- 微信小程序报错:navigateTo:fail can not navigateTo a tabbar page的解决方法
- 「无服务器架构」动手操作Knative -第二部分
- 拆t420s屏轴_手工改造 ThinkPad T420s升级FHD IPS屏
- HTC ruu 解密 工具编译 提取解密key 自行解密 流程记录
- oracle周中的日无效,【填坑】ORA-01846 周中的日无效
- 图片去水印软件-图片去水印哪个软件好用
- React的父子组件通信
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)...
热门文章
- CSS每日学习笔记(0)
- 远程服务器端口是否开放(审计)
- 2017.4.07 js 中的function 实现的方式
- 根据文件扩展名获取系统图标
- SQL2012 附加数据库提示5120错误解决方法
- 计算机网络(二)-性能指标
- android okhttpclient设置编码,Android之okhttp实现socket通讯(非原创)
- mysql normsinv_在MySQL中实现NORMSINV函数
- c++查找pair,使用map,unordered_map,vector
- dbref java_查询mongodb dbref内部字段