Java Json压缩工具类

在Java中处理JSON数据时,压缩技术可有效减少网络传输和存储开销。

Java Json压缩工具类

java 复制代码
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.Base64;

public class JsonCompressorUtil {

    public static void main(String[] args) {
        try {
            String originalJson = "[{\"children\":[{\"children\":[{\"children\":[{\"children\":[{\"children\":[]," +
                    "\"id\":39,\"name\":\"?1\",\"orderNum\":1,\"parentId\":15},{\"children\":[],\"id\":40,\"name\":" +
                    "\"李2\",\"orderNum\":2,\"parentId\":15}],\"id\":15,\"name\":\"人力部\",\"orderNum\":1,\"parentId\":7}," +
                    "{\"children\":[{\"children\":[],\"id\":41,\"name\":\"王3\",\"orderNum\":1,\"parentId\":16},{\"children\":[]," +
                    "\"id\":42,\"name\":\"?4\",\"orderNum\":2,\"parentId\":16}],\"id\":16,\"name\":\"??部\",\"orderNum\":2," +
                    "\"parentId\":7},{\"children\":[{\"children\":[],\"id\":43,\"name\":\"?5\",\"orderNum\":1,\"parentId\":17}," +
                    "{\"children\":[],\"id\":44,\"name\":\"周6\",\"orderNum\":2,\"parentId\":17}],\"id\":17,\"name\":\"后勤部\"," +
                    "\"orderNum\":3,\"parentId\":7}],\"id\":7,\"name\":\"??地区\",\"orderNum\":1,\"parentId\":3},{\"children\":" +
                    "[{\"children\":[{\"children\":[],\"id\":45,\"name\":\"?7\",\"orderNum\":1,\"parentId\":18},{\"children\":[]," +
                    "\"id\":46,\"name\":\"?8\",\"orderNum\":2,\"parentId\":18}],\"id\":18,\"name\":\"人力部\",\"orderNum\":1," +
                    "\"parentId\":8},{\"children\":[{\"children\":[],\"id\":47,\"name\":\"?9\",\"orderNum\":1,\"parentId\":19}," +
                    "{\"children\":[],\"id\":48,\"name\":\"李10\",\"orderNum\":2,\"parentId\":19}],\"id\":19,\"name\":\"??部\"," +
                    "\"orderNum\":2,\"parentId\":8},{\"children\":[{\"children\":[],\"id\":49,\"name\":\"王11\",\"orderNum\":1," +
                    "\"parentId\":20},{\"children\":[],\"id\":50,\"name\":\"?12\",\"orderNum\":2,\"parentId\":20}],\"id\":20," +
                    "\"name\":\"后勤部\",\"orderNum\":3,\"parentId\":8}],\"id\":8,\"name\":\"?南地区\",\"orderNum\":2,\"parentId\":" +
                    "3}],\"id\":3,\"name\":\"??\",\"orderNum\":1,\"parentId\":1},{\"children\":[{\"children\":[{\"children\":" +
                    "[{\"children\":[],\"id\":51,\"name\":\"?13\",\"orderNum\":1,\"parentId\":21},{\"children\":[],\"id\":52," +
                    "\"name\":\"周14\",\"orderNum\":2,\"parentId\":21}],\"id\":21,\"name\":\"人力部\",\"orderNum\":1,\"parentId\":9}," +
                    "{\"children\":[{\"children\":[],\"id\":53,\"name\":\"?15\",\"orderNum\":1,\"parentId\":22},{\"children\":" +
                    "[],\"id\":54,\"name\":\"?16\",\"orderNum\":2,\"parentId\":22}],\"id\":22,\"name\":\"??部\",\"orderNum\":2," +
                    "\"parentId\":9},{\"children\":[{\"children\":[],\"id\":55,\"name\":\"?17\",\"orderNum\":1,\"parentId\":23}," +
                    "{\"children\":[],\"id\":56,\"name\":\"李18\",\"orderNum\":2,\"parentId\":23}],\"id\":23,\"name\":\"后勤部\"," +
                    "\"orderNum\":3,\"parentId\":9}],\"id\":9,\"name\":\"??地区\",\"orderNum\":1,\"parentId\":4},{\"children\":" +
                    "[{\"children\":[{\"children\":[],\"id\":57,\"name\":\"王19\",\"orderNum\":1,\"parentId\":24},{\"children\":" +
                    "[],\"id\":58,\"name\":\"?20\",\"orderNum\":2,\"parentId\":24}],\"id\":24,\"name\":\"人力部\",\"orderNum\":1," +
                    "\"parentId\":10},{\"children\":[{\"children\":[],\"id\":59,\"name\":\"?21\",\"orderNum\":1,\"parentId\":25}," +
                    "{\"children\":[],\"id\":60,\"name\":\"周22\",\"orderNum\":2,\"parentId\":25}],\"id\":25,\"name\":\"??部\"," +
                    "\"orderNum\":2,\"parentId\":10},{\"children\":[{\"children\":[],\"id\":61,\"name\":\"?23\",\"orderNum\":1," +
                    "\"parentId\":26},{\"children\":[],\"id\":62,\"name\":\"?24\",\"orderNum\":2,\"parentId\":26}],\"id\":26," +
                    "\"name\":\"后勤部\",\"orderNum\":3,\"parentId\":10}],\"id\":10,\"name\":\"?南地区\",\"orderNum\":2,\"parentId\":4}]," +
                    "\"id\":4,\"name\":\"小米\",\"orderNum\":2,\"parentId\":1}],\"id\":1,\"name\":\"中国\",\"orderNum\":1,\"parentId\":0}," +
                    "{\"children\":[{\"children\":[{\"children\":[{\"children\":[{\"children\":[],\"id\":63,\"name\":" +
                    "\"Alice1\",\"orderNum\":1,\"parentId\":27},{\"children\":[],\"id\":64,\"name\":\"Bob2\",\"orderNum\":2," +
                    "\"parentId\":27}],\"id\":27,\"name\":\"HR\",\"orderNum\":1,\"parentId\":11},{\"children\":[{\"children\":[]," +
                    "\"id\":65,\"name\":\"Charlie3\",\"orderNum\":1,\"parentId\":28},{\"children\":[],\"id\":66,\"name\":\"David4\"," +
                    "\"orderNum\":2,\"parentId\":28}],\"id\":28,\"name\":\"Development\",\"orderNum\":2,\"parentId\":11},{\"children\":" +
                    "[{\"children\":[],\"id\":67,\"name\":\"Eve5\",\"orderNum\":1,\"parentId\":29},{\"children\":[],\"id\":68,\"name\":" +
                    "\"Frank6\",\"orderNum\":2,\"parentId\":29}],\"id\":29,\"name\":\"Logistics\",\"orderNum\":3,\"parentId\":11}]," +
                    "\"id\":11,\"name\":\"?盛?地区\",\"orderNum\":1,\"parentId\":5},{\"children\":[{\"children\":[{\"children\":[]," +
                    "\"id\":69,\"name\":\"Grace7\",\"orderNum\":1,\"parentId\":30},{\"children\":[],\"id\":70,\"name\":\"Helen8\"," +
                    "\"orderNum\":2,\"parentId\":30}],\"id\":30,\"name\":\"HR\",\"orderNum\":1,\"parentId\":12},{\"children\":" +
                    "[{\"children\":[],\"id\":71,\"name\":\"Alice9\",\"orderNum\":1,\"parentId\":31},{\"children\":[],\"id\":72," +
                    "\"name\":\"Bob10\",\"orderNum\":2,\"parentId\":31}],\"id\":31,\"name\":\"Development\",\"orderNum\":2," +
                    "\"parentId\":12},{\"children\":[{\"children\":[],\"id\":73,\"name\":\"Charlie11\",\"orderNum\":1," +
                    "\"parentId\":32},{\"children\":[],\"id\":74,\"name\":\"David12\",\"orderNum\":2,\"parentId\":32}]," +
                    "\"id\":32,\"name\":\"Logistics\",\"orderNum\":3,\"parentId\":12}],\"id\":12,\"name\":\"??地区\",\"orderNum" +
                    "\":2,\"parentId\":5}],\"id\":5,\"name\":\"谷歌\",\"orderNum\":1,\"parentId\":2},{\"children\":[{\"children" +
                    "\":[{\"children\":[{\"children\":[],\"id\":75,\"name\":\"Eve13\",\"orderNum\":1,\"parentId\":33},{\"children" +
                    "\":[],\"id\":76,\"name\":\"Frank14\",\"orderNum\":2,\"parentId\":33}],\"id\":33,\"name\":\"HR\",\"orderNum\":" +
                    "1,\"parentId\":13},{\"children\":[{\"children\":[],\"id\":77,\"name\":\"Grace15\",\"orderNum\":1,\"parentId\":34}" +
                    ",{\"children\":[],\"id\":78,\"name\":\"Helen16\",\"orderNum\":2,\"parentId\":34}],\"id\":34,\"name\":\"Development\"," +
                    "\"orderNum\":2,\"parentId\":13},{\"children\":[{\"children\":[],\"id\":79,\"name\":\"Alice17\",\"orderNum\":1," +
                    "\"parentId\":35},{\"children\":[],\"id\":80,\"name\":\"Bob18\",\"orderNum\":2,\"parentId\":35}],\"id\":35,\"name\"" +
                    ":\"Logistics\",\"orderNum\":3,\"parentId\":13}],\"id\":13,\"name\":\"?盛?地区\",\"orderNum\":1,\"parentId\":6}," +
                    "{\"children\":[{\"children\":[{\"children\":[],\"id\":81,\"name\":\"Charlie19\",\"orderNum\":1,\"parentId\":36}," +
                    "{\"children\":[],\"id\":82,\"name\":\"David20\",\"orderNum\":2,\"parentId\":36}],\"id\":36,\"name\":\"HR\",\"" +
                    "orderNum\":1,\"parentId\":14},{\"children\":[{\"children\":[],\"id\":83,\"name\":\"Eve21\",\"orderNum\":1,\"" +
                    "parentId\":37},{\"children\":[],\"id\":84,\"name\":\"Frank22\",\"orderNum\":2,\"parentId\":37}],\"id\":37,\"" +
                    "name\":\"Development\",\"orderNum\":2,\"parentId\":14},{\"children\":[{\"children\":[],\"id\":85,\"name\":\"" +
                    "Grace23\",\"orderNum\":1,\"parentId\":38},{\"children\":[],\"id\":86,\"name\":\"Helen24\",\"orderNum\":2,\"" +
                    "parentId\":38}],\"id\":38,\"name\":\"Logistics\",\"orderNum\":3,\"parentId\":14}],\"id\":14,\"name\":\"??地区\"," +
                    "\"orderNum\":2,\"parentId\":6}],\"id\":6,\"name\":\"微?\",\"orderNum\":2,\"parentId\":2}],\"id\":2,\"name\":\"美国\"," +
                    "\"orderNum\":2,\"parentId\":0}]\n";

            System.out.println("原始JSON长度: " + originalJson.length());

            String compressed = JsonCompressorUtil.compressToBase64(originalJson);
            System.out.println("压缩后Base64长度: " + compressed.length());
            System.out.println("压缩率: " + (100 - (compressed.length() * 100 / originalJson.length())) + "%");

            String decompressed = JsonCompressorUtil.decompressFromBase64(compressed);
            System.out.println("解压后内容是否一致: " + originalJson.equals(decompressed));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 压缩JSON字符串为Base64编码的GZIP数据
     *
     * @param jsonString 原始JSON字符串
     * @return Base64编码的压缩字符串
     * @throws IOException 压缩异常
     */
    public static String compressToBase64(String jsonString) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (GZIPOutputStream gzipOS = new GZIPOutputStream(bos)) {
            gzipOS.write(jsonString.getBytes("UTF-8"));
        }
        return Base64.getEncoder().encodeToString(bos.toByteArray());
    }

    /**
     * 解压Base64编码的GZIP数据为JSON字符串
     *
     * @param compressedBase64 压缩后的Base64字符串
     * @return 原始JSON字符串
     * @throws IOException 解压异常
     */
    public static String decompressFromBase64(String compressedBase64) throws IOException {
        byte[] compressedBytes = Base64.getDecoder().decode(compressedBase64);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = gzipIS.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        }
        return bos.toString("UTF-8");
    }

    /**
     * 直接压缩JSON字符串为字节数组
     *
     * @param jsonString 原始JSON字符串
     * @return 压缩后的字节数组
     * @throws IOException 压缩异常
     */
    public static byte[] compress(String jsonString) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (GZIPOutputStream gzipOS = new GZIPOutputStream(bos)) {
            gzipOS.write(jsonString.getBytes("UTF-8"));
        }
        return bos.toByteArray();
    }

    /**
     * 直接解压字节数组为JSON字符串
     *
     * @param compressedBytes 压缩后的字节数组
     * @return 原始JSON字符串
     * @throws IOException 解压异常
     */
    public static String decompress(byte[] compressedBytes) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = gzipIS.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        }
        return bos.toString("UTF-8");
    }

}

每天⽤⼼记录⼀点点。内容也许不重要,但习惯很重要!

干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!

相关推荐
霸道流氓气质1 分钟前
Java中信号量(Semaphore):从本地到分布式
java·开发语言·分布式
怪奇云呼军3 分钟前
G.711、Opus 和重采样会拖慢识别吗?闪电智能VoiceAgent 的音频入口怎么选
java·人工智能·python·算法·云计算·音视频
ZC跨境爬虫16 分钟前
LeetCode 27. 移除元素(双指针详解 + Java Python 多解法对比)
java·python·leetcode
kisbad1 小时前
Day 036|OpenAI Agents SDK 快速开始:今天跑通第一个 Agent
java·前端·javascript
憧憬成为java架构高手的小白1 小时前
langchain4j框架学习
java
0x531 小时前
Java网络编程(四)
java·网络
ly76892 小时前
Java 设计模式详解:从原则到 23 种经典模式
java·开发语言·设计模式
萧瑟余晖2 小时前
Java深入解析篇三十之Java日志框架
java·开发语言
Suxing93 小时前
C语言基础分享:从“租房”到“拆迁”,C语言动态内存管理
java·数据结构·算法
减瓦3 小时前
别让 Java 版本选择变成生产事故
java