log4j-1.2.17.jar
slf4j-api-1.7.36.jar
slf4j-log4j12-1.7.7.jar
commons-lang3-3.13.0.jar
commons-io-2.14.0.jar
commons-net-3.10.0.jar
基于 JDK 1.8
package com.chenwc.gzip;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.*;
/**
* 文本压缩工具类
* @author chenwc
* @date 2023/10/14 14:28
*/
public class GzipUtils {
private static final Logger log = LoggerFactory.getLogger(GzipUtils.class);
/**
* 使用gzip进行压缩
*
* @param str 压缩前的文本
* @return 返回压缩后的文本
*/
public static String compress(String str) {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = null;
GZIPOutputStream gzip = null;
try {
out = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(gzip);
}
return Base64.encodeBase64String(out.toByteArray());
}
/**
* 使用gzip进行解压缩
*
* @param str 压缩后的文本
* @return 解压后的字文本
*/
public static String uncompress(String str) {
if (str == null || str.length() == 0) {
return str;
}
Base64.decodeBase64(str);
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
GZIPInputStream gunzip = null;
String ret = null;
try {
in = new ByteArrayInputStream(Base64.decodeBase64(str));
gunzip = new GZIPInputStream(in);
out = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
ret = out.toString(String.valueOf(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(gunzip);
}
return ret;
}
/**
* 使用zip进行压缩
*
* @param str 压缩前的文本
* @return 返回压缩后的文本
*/
public static String zip(String str) {
if (StringUtils.isEmpty(str))
return null;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
String ret = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out, StandardCharsets.UTF_8);
zout.putNextEntry(new ZipEntry("0"));
IOUtils.write(str.getBytes(StandardCharsets.UTF_8), zout);
zout.closeEntry();
ret = Base64.encodeBase64String(out.toByteArray());
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(zout);
IOUtils.closeQuietly(out);
}
return ret;
}
/**
* 使用zip进行解压缩
*
* @param compressedStr 压缩后的文本
* @return 解压后的字文本
*/
public static String unzip(String compressedStr) {
if (StringUtils.isEmpty(compressedStr)) {
return null;
}
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String ret = null;
try {
byte[] compressed = Base64.decodeBase64(compressedStr);
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in, StandardCharsets.UTF_8);
zin.getNextEntry();
ret = IOUtils.toString(zin, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(zin);
IOUtils.closeQuietly(in);
}
return ret;
}
public static void main(String[] args) {
String bbString = "{\"ESB\":{\"DATA\":{\"DATAINFOS\":{\"PUUID\":\"6299864b630142839d270dbdb15616db\",\"DATAINFO\":[{\"DESC18\":\"ejdw_zslc_mdm\",\"DESC14\":\"刘杰强\",\"DESC36\":\"有限责任公司(外商投资、非独资)\",\"DESC15\":\"法人\",\"DESC10\":\"天津市\",\"DESC54\":\"1\",\"DESC32\":\"010114\",\"DESC11\":\"人民币\",\"DESC33\":\"内资公司\",\"DESC12\":\"911201166009142877\",\"DESC34\":\"营利法人\",\"DESC35\":\"企业\",\"DESC13\":\"2408312576\",\"DESC52\":\"1\",\"DESC30\":\"120000\",\"DESC53\":\"0\",\"DESC9\":\"天津市\",\"DESC7\":\"156\",\"DESC8\":\"中华人民共和国\",\"DESC6\":\"天津自贸试验区(天津港保税区)天保大道188号开利大厦903室\",\"DESC3\":\"关联\",\"UUID\":\"0f472b9941a447e69f01da7f9cb7be4c\",\"DESC4\":\"有限责任公司(外商投资、非独资)\",\"DESC1\":\"天津港集装箱码头有限公司\",\"DESC2\":\"境内\",\"DESC29\":\"120000\",\"DESC25\":\"孙凯\",\"MULTICODE\":{\"VALUELIST\":[{\"SPECIALITYCODE\":\"D01\",\"DESC14\":\"\",\"DESC15\":\"\",\"DESC16\":\"\",\"DESC17\":\"\",\"DESC11\":\"中华人民共和国\",\"DESC12\":\"CN\",\"DESC9\":\"A\",\"DESC5\":\"CNY\",\"DESC6\":\"人民币\",\"DESC3\":\"中国工商银行股份有限公司天津天保支行\",\"DESC4\":\"102110000041\",\"DESC1\":\"天津港集装箱码头有限公司\",\"DESC2\":\"0302092109105530872\"},{\"SPECIALITYCODE\":\"D01\",\"DESC14\":\"\",\"DESC15\":\"\",\"DESC16\":\"\",\"DESC17\":\"\",\"DESC11\":\"中华人民共和国\",\"DESC12\":\"CN\",\"DESC9\":\"A\",\"DESC5\":\"CNY\",\"DESC6\":\"人民币\",\"DESC3\":\"中国银行股份有限公司天津滨海分行\",\"DESC4\":\"104110050005\",\"DESC1\":\"天津港集装箱码头有限公司\",\"DESC2\":\"272660054983\"},{\"SPECIALITYCODE\":\"B02\",\"LISTCODE\":\"e6fc3ae175ff45c49712ddceb1c1c103\",\"DESC3\":\"houwenbin@tctcn.com\",\"DESC1\":\"刘杰强\",\"DESC2\":\"022-66276950\"},{\"SPECIALITYCODE\":\"F03\",\"LISTCODE\":\"1e69c122d6334847a33743bd7fe439bc\",\"DESC3\":\"http://10.8.51.1:9000/mdm-masterdata-business/cloud/partner/downloadFile?fileKey=4c7fe344bcca4528ab548bc870359351\",\"DESC1\":\"pdf\",\"DESC2\":\"集装箱发票.pdf\"}]},\"DESC26\":\"天津港集装箱码头有限公司\",\"DESC27\":\"元\",\"DESC28\":\"CNY\",\"DESC21\":\"d9ef9df00edf42aaa3ce059e1a08fc79\",\"DESC22\":\"A\",\"DESC45\":\"天津港集装箱码头有限公司\",\"DESC23\":\"911201166009142877\",\"DESC24\":\"2408312700\",\"DESC40\":\"4502\",\"CODE\":\"10154076\",\"DESC20\":\"匹配失败,与企智校验结果不一致\"}],\"SYNCODE\":\"gys_ejdw_zslc_mdm_to_mdm\"}}}}";
String compressStr = zip(bbString);
//log.info("zip 压缩后的字符串:{}", compressStr);
log.info("zip 解压缩后的字符串:{}", unzip(compressStr));
String compress = compress(bbString);
//log.info("gzip 压缩后的字符串:{}", compress);
log.info("gzip 解压缩后的字符串:{}", uncompress(compress));
}
}