片段 1

片段 2

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 ]的更多相关文章

  1. 文件压缩、解压工具类。文件压缩格式为zip

    package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...

  2. Linux打包压缩解压工具

    第1章      Linux 打包压缩解压工具一.压缩.解压工具 compress/uncompress gzip/gunzip bzip2/bunzip2/ bzcat xz/unxz/ xzcat ...

  3. GZIP压缩、解压缩工具类

    GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...

  4. 使用SharpZIpLib写的压缩解压操作类

    使用SharpZIpLib写的压缩解压操作类,已测试. public class ZipHelper { /// <summary> /// 压缩文件 /// </summary&g ...

  5. Java_压缩与解压工具类

    转载请注明出处:http://blog.csdn.net/y22222ly/article/details/52201675 zip压缩,解压 zip压缩与解压主要依靠java api的两个类: Zi ...

  6. 字符串GZIP压缩解压

    c# /// <summary> /// 字符串压缩解压 /// </summary> public class Zipper { public static string C ...

  7. Zip包解压工具类

    最近在做项目的自动检测离线升级,使用到了解压zip包的操作,本着拿来主义精神,搞了个工具类(同事那边拿的),用着还不错. package com.winning.polaris.admin.utils ...

  8. zip文件解压工具类

    java解压zip文件 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...

  9. [No0000DF]C# ZipFileHelper ZIP类型操作,压缩解压 ZIP 类封装

    using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...

随机推荐

  1. 牛客网数据库SQL实战解析(21-30题)

    牛客网SQL刷题地址: https://www.nowcoder.com/ta/sql?page=0 牛客网数据库SQL实战解析(01-10题): https://blog.csdn.net/u010 ...

  2. 从零开始的SpringBoot项目 ( 一 ) Spring、SpringMVC和SpringBoot

    要想开始一个SpringBoot项目 , 首先要知道SpringBoot是什么 , 可以做什么 , 再去研究怎么用 . 一.概念 1.Spring Spring是一个开源容器框架,可以接管web层,业 ...

  3. CF1203D2 Remove the Substring (hard version) 题解

    这题初赛让我白给了6分,于是我决定回来解决一下它. 说实话,看原题题面和看CCF代码真是两种完全不同的感受…… ------------思路分析: 把$s$串删去一部分之后,会把$s$串分成两部分,当 ...

  4. 百度统计可以查看用户IP

    http://www.wocaoseo.com/thread-123-1-1.html 本文来源于百度官方报道,据悉百度统计披露了访客IP地址,小编乐不可支.比起之前欲说还休的访客标识码,百度统计这次 ...

  5. Redis高可用——副本机制

    目录 概念 配置 同步方式 起点 主从握手 部分同步 完全同步 执行完全同步判断条件 完全同步代码实现 为实现Redis服务的高可用,Redis官方为我们提供了副本机制(或称主从复制)和哨兵机制.副本 ...

  6. DataPicker 日期选择器

    问题描述:DataPicker 日期选择器,如何选取时间段(本日,昨日,上周,上月,当月) 代码实现: <el-date-picker v-model="searchForm.data ...

  7. ORA-01804报错

    报错Error while trying to retrieve text for error ORA-01804 环境变量有配https://blog.csdn.net/zklth/article/ ...

  8. 喵的Unity游戏开发之路 - 游泳

    原文: https://mp.weixin.qq.com/s/-ERFNB1GRZ6UAkHOhP9UQw 很多童鞋没有系统的Unity3D游戏开发基础,也不知道从何开始学.为此我们精选了一套国外优秀 ...

  9. Android反解符号表工具

    cd ~/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin ./arm-linux-an ...

  10. leetcode-双指针遍历

    编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 char[] 的形式给出.不要给另外的数组分配额外的空间,你必须原地修改输入数组.使用 O(1) 的额外空间解决这一问题.你可以假设 ...