GZip 压缩解压 工具类 [ GZipUtil ]
pom.xml
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
GZipUtil.java
package com.app.core.util;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.codec.binary.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@Log4j2
public class GZipUtil {
/**
* 压缩GZip
*
* @return String
*/
public static String gZip(String input) {
byte[] bytes = null;
GZIPOutputStream gzip = null;
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(bos);
gzip.write(input.getBytes(CharEncoding.UTF_8));
gzip.finish();
gzip.close();
bytes = bos.toByteArray();
bos.close();
} catch (Exception e) {
log.error("压缩出错:", e);
} finally {
try {
if (gzip != null)
gzip.close();
if (bos != null)
bos.close();
} catch (final IOException ioe) {
log.error("压缩出错:", ioe);
}
}
return Base64.encodeBase64String(bytes);
}
/**
* 解压GZip
*
* @return String
*/
public static String unGZip(String input) {
byte[] bytes;
String out = input;
GZIPInputStream gzip = null;
ByteArrayInputStream bis;
ByteArrayOutputStream bos = null;
try {
bis = new ByteArrayInputStream(Base64.decodeBase64(input));
gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num;
bos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, num);
}
bytes = bos.toByteArray();
out = new String(bytes, CharEncoding.UTF_8);
gzip.close();
bis.close();
bos.flush();
bos.close();
} catch (Exception e) {
log.error("解压出错:", e);
} finally {
try {
if (gzip != null)
gzip.close();
if (bos != null)
bos.close();
} catch (final IOException ioe) {
log.error("解压出错:", ioe);
}
}
return out;
}
}
GZip 压缩解压 工具类 [ GZipUtil ]的更多相关文章
- 文件压缩、解压工具类。文件压缩格式为zip
package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...
- Linux打包压缩解压工具
第1章 Linux 打包压缩解压工具一.压缩.解压工具 compress/uncompress gzip/gunzip bzip2/bunzip2/ bzcat xz/unxz/ xzcat ...
- GZIP压缩、解压缩工具类
GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...
- 使用SharpZIpLib写的压缩解压操作类
使用SharpZIpLib写的压缩解压操作类,已测试. public class ZipHelper { /// <summary> /// 压缩文件 /// </summary&g ...
- Java_压缩与解压工具类
转载请注明出处:http://blog.csdn.net/y22222ly/article/details/52201675 zip压缩,解压 zip压缩与解压主要依靠java api的两个类: Zi ...
- 字符串GZIP压缩解压
c# /// <summary> /// 字符串压缩解压 /// </summary> public class Zipper { public static string C ...
- Zip包解压工具类
最近在做项目的自动检测离线升级,使用到了解压zip包的操作,本着拿来主义精神,搞了个工具类(同事那边拿的),用着还不错. package com.winning.polaris.admin.utils ...
- zip文件解压工具类
java解压zip文件 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...
- [No0000DF]C# ZipFileHelper ZIP类型操作,压缩解压 ZIP 类封装
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
随机推荐
- Java类库以及它的基本组织结构
Java 类库概念: Java 的应用程序编程接口 (API (Application Programming Interface) )以包的形式来组织,每个包提供了大量的相关类.接口和异常处理类, ...
- iOS多线程之GCD、OperationQueue 对比和实践记录
[toc] 简介 在计算的早期,计算机可以执行的最大工作量是由 CPU 的时钟速度决定的.但是随着技术的进步和处理器设计的紧凑化,热量和其他物理约束开始限制处理器的最大时钟速度.因此,芯片制 ...
- 从request中获取文件流的两种方式,配置文件上传大小
原文地址:https://blog.csdn.net/xyr05288/article/details/80692132
- PCIe例程理解(一)用户逻辑模块(接收)仿真分析
前言 本文从例子程序细节上(语法层面)去理解PCIe对于事物层数据的接收及解析. 参考数据手册:PG054: 例子程序有Vivado生成: 为什么将这个内容写出来? 通过写博客,可以检验自己理解了这个 ...
- 从APP的启动说起
iOS里面APP的启动,过程有些复杂,今天我们来抽丝剥茧,一步步探讨一下APP的启动会经历哪些过程. 首先,用户点击iPhone里面的某个APP的icon,Kernel内核会开始初始化空间并创建进程, ...
- Intermediate English Book 1
List x1.0 x1.5 Lesson 1 Reading Lesson 1 Details Lesson 2 Dialogue Lesson 2 Details Lesson 3 Reading ...
- Unity坐标系详解
1. World Space(世界坐标系): 我们在场景中添加的物体(如:Cube),他们都是以世界坐标显示在场景中.transform.position 获取的便是这个 坐标数值. 2. Scene ...
- 【HttpRunner v3.x】笔记—8.用例引用、变量传递
看到这里,对于httprunner已经有了一个大概的了解,现在想对于一些比较重要或者常用的功能,进行一些实践操作. 毕竟那谁说过,"纸上得来终觉浅,绝知此事要躬行." 上一篇提到了 ...
- es6使用箭头函数需要注意的问题
this问题 箭头函数没有它自己的this值,箭头函数内的this值继承自外围作用域,谁定义的函数,this指向谁 箭头函数要实现类似纯函数的效果,必须剔除外部状态.所以箭头函数不具备普通函数里常见的 ...
- JS中有趣的内置对象-JSON
前言 在以前的web开发中,我们多数选择纯文本或XML作为我们的提交的数据格式,大多数是XML,少数纯文本.其实从AJAX(Asynchronous JavaScript and XML)的命名我们也 ...