java字符串压缩和字符串解压
运行效果
java工具类
CompressUtil.java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.codec.binary.Base64;import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;public class CompressUtil {/*** 字符串的压缩** @param str 待压缩的字符串* @return 返回压缩后的字符串* @throws IOException*/public static String compress(String str) throws IOException {if (null == str || str.length() <= 0) {return null;}// 创建一个新的 byte 数组输出流ByteArrayOutputStream out = new ByteArrayOutputStream();// 使用默认缓冲区大小创建新的输出流GZIPOutputStream gzip = new GZIPOutputStream(out);// 将 b.length 个字节写入此输出流gzip.write(str.getBytes("UTF-8"));gzip.close();// 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串return out.toString("ISO-8859-1");}public static String compressEncode(Object res) throws IOException {String json = JSON.toJSONString(res, SerializerFeature.DisableCircularReferenceDetect);String com = compress(json);if (null == com) {return null;}String base = Base64.encodeBase64String(com.getBytes("ISO-8859-1"));return base;}// 压缩字符串public static String compressEncode(String res) throws IOException {String com = compress(res);if (null == com) {return null;}String base = Base64.encodeBase64String(com.getBytes("ISO-8859-1"));return base;}// 解压字符串public static String decompressEncode(String zipString) {byte[] bArr = Base64.decodeBase64(zipString);try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);GZIPInputStream gZIPInputStream = new GZIPInputStream(byteArrayInputStream);BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gZIPInputStream, StandardCharsets.UTF_8))) {StringBuilder sb2 = new StringBuilder();String readLine;while ((readLine = bufferedReader.readLine()) != null) {sb2.append(readLine);}return sb2.toString();} catch (Exception e) {e.printStackTrace();}return null;}public static void main(String[] args) throws IOException {String a = "[{\"imgBase64\":\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAC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AAASUVORK5CYII=\",\"tileNum\":\"219542_107123_18\"},{\"imgBase64\":\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAAAXNSR0IArs4c6QAABlJJREFUeF7t1AENACAMA0GwhCj8GyCQYONvDnpdOu9ZezgCBJIC0wAkexeawBcwAB6BQFjAAITLF52AAfADBMICBiBcvugEDIAfIBAWMADh8kUnYAD8AIGwgAEIly86AQPgBwiEBQxAuHzRCRgAP0AgLGAAwuWLTsAA+AECYQEDEC5fdAIGwA8QCAsYgHD5ohMwAH6AQFjAAITLF52AAfADBMICBiBcvugEDIAfIBAWMADh8kUnYAD8AIGwgAEIly86AQPgBwiEBQxAuHzRCRgAP0AgLGAAwuWLTsAA+AECYQEDEC5fdAIGwA8QCAsYgHD5ohMwAH6AQFjAAITLF52AAfADBMICBiBcvugEDIAfIBAWMADh8kUnYAD8AIGwgAEIly86AQPgBwiEBQxAuHzRCRgAP0AgLGAAwuWLTsAA+AECYQEDEC5fdAIGwA8QCAsYgHD5ohMwAH6AQFjAAITLF52AAfADBMICBiBcvugEDIAfIBAWMADh8kUnYAD8AIGwgAEIly86AQPgBwiEBQxAuHzRCRgAP0AgLGAAwuWLTsAA+AECYQEDEC5fdAIGwA8QCAsYgHD5ohMwAH6AQFjAAITLF52AAfADBMICBiBcvugEDIAfIBAWMADh8kUnYAD8AIGwgAEIly86AQPgBwiEBQxAuHzRCRgAP0AgLGAAwuWLTsAA+AECYQEDEC5fdAIGwA8QCAsYgHD5ohMwAH6AQFjAAITLF52AAfADBMICBiBcvugEDIAfIBAWMADh8kUnYAD8AIGwgAEIly86AQPgBwiEBQxAuHzRCRgAP0AgLGAAwuWLTsAA+AECYQEDEC5fdAIGwA8QCAsYgHD5ohMwAH6AQFjAAITLF52AAfADBMICBiBcvugEDIAfIBAWMADh8kUnYAD8AIGwgAEIly86AQPgBwiEBQxAuHzRCRgAP0AgLGAAwuWLTsAA+AECYQEDEC5fdAIGwA8QCAsYg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AWMADh8kUnYAD8AIGwgAEIly86AQPgBwiEBQxAuHzRCRgAP0AgLGAAwuWLTsAA+AECYQEDEC5fdAIPf96VH4apL1MAAAAASUVORK5CYII=\",\"tileNum\":\"219542_107124_18\"}]";System.out.println("原始字符串长度:" + a.length() + " 内容:" + a);String compressEncode = compressEncode(a);System.out.println("压缩字符串长度:" + compressEncode.length() + " 内容:" + compressEncode);String decompressEncode = decompressEncode(compressEncode);System.out.println("解压字符串长度:" + decompressEncode.length() + " 内容:" + decompressEncode);}
}
https://blog.csdn.net/weixin_42274229/article/details/124796502