片段 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. Java 接口 抽象类 抽象方法

    abstract class elehousekeeping { //抽象家用电器类 abstract void opermode(); //抽象方法} class TV extends elehou ...

  2. 人到中年的程序员,请提前准备好 Plan B

    中年程序员的生存现状已经是老生常谈的话题了,有多老呢?十年前,就有一位名叫"johnfx"的程序员谈过这个话题,并且专门为此写了一篇文章.随着中年程序员生存现状的话题再次成为热点, ...

  3. element UI 上传文件成功后 - 清空文件

    request({ url: '/jiekou', method: 'post', data }).then(res => { this.$message({ type: 'success', ...

  4. 【转】Mac下Eclipse快捷键

    http://blog.sina.com.cn/s/blog_677089db01019jgh.html Command + O:显示大纲Command + 1:快速修复Command + D:删除当 ...

  5. jdk1.8 新增工具类

    目录 optional 时间API Instant localDateTime LocalDate LocalTime Duration TemporalAdjuster DateTimeFormat ...

  6. Google Kick Start 2020 Round C

    ac代码 A. Countdown for循环跑一跑,没啥好说的. B. Stable Wall 如果\(s_{i,j} \ne s_{i+1,j}\),那么说明\(s_{i+1,j}\)必须在\(s ...

  7. 转载:51cto 2019好文精选

    转载地址:https://news.51cto.com/art/202001/609544.htm 01.知识科普 傻瓜都能看懂,30张图彻底理解红黑树! TCP三次握手,四次挥手,你真的懂吗? 面试 ...

  8. linux vi编辑

    编辑模式 使用vi进入文本后,按i开始编辑文本 退出编辑模式 按ESC键,然后: 退出vi :q! 不保存文件,强制退出vi命令 :w 保存文件,不退出vi命令 :wq 保存文件,退出vi命令 中断v ...

  9. leetcode刷题-88.合并两个有序数组

    题目 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  10. 2020BJDCTF

    diff: 不得不说这种题目挺有意思的,现在记录一下阶梯过程: 先登录远程,发现有两个文件: 虽然直接能卡到flag文件,但是我们是以ctf用户登录的,并不能直接打开flag文件.仔细观察diff文件 ...