片段 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数据结构——AVL树

    AVL树(平衡二叉树)定义 AVL树本质上是一颗二叉查找树,但是它又具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树,并且拥有自平衡机制.在AV ...

  2. java安全编码指南之:基础篇

    目录 简介 java平台本身的安全性 安全第一,不要写聪明的代码 在代码设计之初就考虑安全性 避免重复的代码 限制权限 构建可信边界 封装 写文档 简介 作为一个程序员,只是写出好用的代码是不够的,我 ...

  3. 超级码力编程赛带着6万奖金和1200件T恤向你跑来了~

    炎炎夏日,总是感觉很疲劳,提不起一点精神怎么办?是时候参加一场比赛来唤醒你的激情了!阿里云超级码力在线编程大赛震撼携手全国数百所高校震撼来袭. 它来了,它来了,它带着60000现金和1200件T恤向你 ...

  4. 基于Appium的UI自动化测试

    为什么需要UI自动化测试 移动端APP是一个复杂的系统,不同功能之间耦合性很强,很难仅通过单元测试保障整体功能.UI测试是移动应用开发中重要的一环,但是执行速度较慢,有很多重复工作量,为了减少这些工作 ...

  5. 图解Janusgraph系列-分布式id生成策略分析

    JanusGraph - 分布式id的生成策略 大家好,我是洋仔,JanusGraph图解系列文章,实时更新~ 本次更新时间:2020-9-1 文章为作者跟踪源码和查看官方文档整理,如有任何问题,请联 ...

  6. docker搭建zabbix收集windows计数器性能数据

    1 docker服务的安装 1)在线安装docker服务 在线安装可以参考下面的安装步骤 a 安装相关依赖组件 yum install -y yum-utils device-mapper-persi ...

  7. package_ios

    PlistBuddy简单使用 https://www.jianshu.com/p/2167f755c47e xcodebuild 命令 https://www.jianshu.com/p/c32263 ...

  8. python3笔记-字典

    5 1 # 创建字典 6 2 d=dict(name='lily',age=18,phone='') 7 3 print(d) 4 # {'name': 'lily', 'age': 18, 'pho ...

  9. Linux 基础指令2

    管道符| 先执行第一个命令后执行第二个 eg:查看文件中间几行: head -n1 文件名| tail -n2 :先查看文件前n1行,然后在n1行中的后n2行 tail -n1 文件名| head - ...

  10. akka-streams - 从应用角度学习:basic stream parts

    实际上很早就写了一系列关于akka-streams的博客.但那个时候纯粹是为了了解akka而去学习的,主要是从了解akka-streams的原理为出发点.因为akka-streams是akka系列工具 ...