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并发---concurrent包
一.包的结构层次 其中包含了两个子包atomic和locks,另外字concurrent下的阻塞队列以及executor,这些就是concurrent包中的精华.而这些类的实现主要是依赖于volati ...
- muduo源码解析8-date类
date class date:copyable { }: 作用: 此类作用主要是实现年月日和julianDay的互相转换内部最重要的一个数据成员m_julianDayNumber在mymuduo:: ...
- C语言中存储多个字符串的两种方式
C语言中存储多个字符串的两种方式 方式一 二维字符串数组 声明: char name[][] = { "Justinian", "Momo", " ...
- Javascript数组与函数初识
1 - 数组 1.1 数组的概念 数组可以把一组相关的数据一起存放,并提供方便的访问(获取)方式. 数组是指一组数据的集合,其中的每个数据被称作元素,在数组中可以存放任意类型的元素.数组是一种将一组数 ...
- Babel知识点相关
本篇是根据最新babel 7版本写的,里面用到的一些babel相关包都是babel 7的 1,babel是如何工作的 babel是一个转译器,这里我严格区分了转译器和编译器,因为编译器最终生成 ...
- Android,java,php开发最基本的知识,mysql sqlite数据库的增删改查代理,sql语句
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985转载请说明出处. 下面是代码: 增加:insert into 数据表(字段1,字段2,字段3) valu ...
- 序列号,IMEI,IMSI,ICCID的含义
什么是序列号? 序列号是一串标识你手机出生证明以及身材特征的信息,甚至还可用来识别是否为官方翻新机.你可以简单的将这一串数字分割为:aabccdddeef 的形式.拿iPhone 4为例 aa = 工 ...
- 使用Unity的50个建议
关于这些建议 这些建议并不适用于所有的项目 这些建议是基于我与3-20人的小团队项目经验总结出来的 结构.可重复使用性.明晰度都是有价的——团队规模和项目规模决定了是否值得付这个价. 一些建议也许公然 ...
- 01_Python基础知识梳理
1.计算机知识基础 1.计算机组成 计算机底层: 点子电路,计算机只能识别两个数 0 1 硬件: 处理器(CPU), 运行内存(RAM), 主板(总线设备), 外部存储设备(硬盘U盘等 ...
- Brup sute
暴力破解