查看: 13|回复: 0

Java中Deflater和GZIP的压缩/解压实现

[复制链接]

4

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-7-1
发表于 2025-6-10 10:21:00 | 显示全部楼层 |阅读模式

以下是Java中DeflaterGZIP的压缩/解压实现及优缺点对比:


一、Deflater实现(原始DEFLATE格式)

1. 压缩方法

    public static String compress(String rawData) {
        Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true); // nowrap=true
        try {
            deflater.setInput(rawData.getBytes(StandardCharsets.UTF_8));
            deflater.finish();

            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                byte[] buffer = new byte[8192];
                while (!deflater.finished()) {
                    int compressedBytes = deflater.deflate(buffer);
                    bos.write(buffer, 0, compressedBytes);
                }
                return Base64.getUrlEncoder().withoutPadding()
                         .encodeToString(bos.toByteArray());
            }
        } finally {
            deflater.end();
        }
    }

2. 解压方法

    public static String decompress(String compressedData) {
        Inflater inflater = new Inflater(true);
        try {
            byte[] compressedBytes = Base64.getUrlDecoder().decode(compressedData);
            inflater.setInput(compressedBytes);

            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                byte[] buffer = new byte[8192];
                while (!inflater.finished()) {
                    try {
                        int decompressedBytes = inflater.inflate(buffer);
                        bos.write(buffer, 0, decompressedBytes);
                    } catch (DataFormatException e) {
                        throw new RuntimeException("数据损坏", e);
                    }
                }
                return bos.toString(StandardCharsets.UTF_8);
            }
        } finally {
            inflater.end();
        }
    }

二、GZIP实现(标准gzip格式)

1. 压缩方法

    public static String compress(String rawData) throws IOException {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
            
            gzip.write(rawData.getBytes(StandardCharsets.UTF_8));
            gzip.finish();
            return Base64.getEncoder().encodeToString(bos.toByteArray());
        }
    }

2. 解压方法

    public static String decompress(String compressedData) throws IOException {
        byte[] compressedBytes = Base64.getDecoder().decode(compressedData);
        try (ByteArrayInputStream bis = new ByteArrayInputStream(compressedBytes);
             GZIPInputStream gzip = new GZIPInputStream(bis);
             ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            
            byte[] buffer = new byte[8192];
            int len;
            while ((len = gzip.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }
            return bos.toString(StandardCharsets.UTF_8);
        }
    }

三、优缺点对比

特性 Deflater (DEFLATE) GZIP
压缩率 ≈95%(无头部,极限压缩) ≈93%(含18字节头部)
速度 稍快(无头部开销) 稍慢(需处理头部)
兼容性 需特殊解析器 通用解压工具支持
典型应用场景 网络传输、内存敏感型应用 文件存储、通用数据交换
头部开销 18字节(含时间戳等元数据)
校验和 有(CRC32)
流式处理 需手动管理缓冲区 支持流式API

四、选型建议

  1. 优先选GZIP的场景

    • 需要与其他系统交互时
    • 处理文件存储或日志压缩时
    • 需要内置校验和验证数据完整性时
  2. 优先选Deflater的场景

    • 追求极限压缩率时
    • 进行网络传输(尤其对延迟敏感时)
    • 需要自定义协议格式时
  3. 通用原则

    • 短文本(<1KB)建议直接存储
    • 中等长度文本(1KB-1MB)优先GZIP
    • 大文本(>1MB)可结合缓冲流处理

实际测试显示,对于典型英文文本,Deflater比GZIP压缩率高约2-5%,但解压速度慢约10-15%。



来源:https://www.cnblogs.com/sailCoding/p/18921716
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部