那些年的梦想之旅 發表於 2025-6-10 10:21:00

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

<h2 id="以下是java中deflater和gzip的压缩解压实现及优缺点对比">以下是Java中<code>Deflater</code>和<code>GZIP</code>的压缩/解压实现及优缺点对比:</h2>
<hr>
<h3 id="一deflater实现原始deflate格式">一、Deflater实现(原始DEFLATE格式)</h3>
<h4 id="1-压缩方法">1. 压缩方法</h4>
<pre><code class="language-java">    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;
                while (!deflater.finished()) {
                  int compressedBytes = deflater.deflate(buffer);
                  bos.write(buffer, 0, compressedBytes);
                }
                return Base64.getUrlEncoder().withoutPadding()
                         .encodeToString(bos.toByteArray());
            }
      } finally {
            deflater.end();
      }
    }
</code></pre>
<h4 id="2-解压方法">2. 解压方法</h4>
<pre><code class="language-java">    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;
                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();
      }
    }
</code></pre>
<hr>
<h3 id="二gzip实现标准gzip格式">二、GZIP实现(标准gzip格式)</h3>
<h4 id="1-压缩方法-1">1. 压缩方法</h4>
<pre><code class="language-java">    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());
      }
    }
</code></pre>
<h4 id="2-解压方法-1">2. 解压方法</h4>
<pre><code class="language-java">    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;
            int len;
            while ((len = gzip.read(buffer)) &gt; 0) {
                bos.write(buffer, 0, len);
            }
            return bos.toString(StandardCharsets.UTF_8);
      }
    }
</code></pre>
<hr>
<h3 id="三优缺点对比">三、优缺点对比</h3>
<table>
<thead>
<tr>
<th>特性</th>
<th>Deflater (DEFLATE)</th>
<th>GZIP</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>压缩率</strong></td>
<td>≈95%(无头部,极限压缩)</td>
<td>≈93%(含18字节头部)</td>
</tr>
<tr>
<td><strong>速度</strong></td>
<td>稍快(无头部开销)</td>
<td>稍慢(需处理头部)</td>
</tr>
<tr>
<td><strong>兼容性</strong></td>
<td>需特殊解析器</td>
<td>通用解压工具支持</td>
</tr>
<tr>
<td><strong>典型应用场景</strong></td>
<td>网络传输、内存敏感型应用</td>
<td>文件存储、通用数据交换</td>
</tr>
<tr>
<td><strong>头部开销</strong></td>
<td>无</td>
<td>18字节(含时间戳等元数据)</td>
</tr>
<tr>
<td><strong>校验和</strong></td>
<td>无</td>
<td>有(CRC32)</td>
</tr>
<tr>
<td><strong>流式处理</strong></td>
<td>需手动管理缓冲区</td>
<td>支持流式API</td>
</tr>
</tbody>
</table>
<hr>
<h3 id="四选型建议">四、选型建议</h3>
<ol>
<li>
<p><strong>优先选GZIP的场景</strong>:</p>
<ul>
<li>需要与其他系统交互时</li>
<li>处理文件存储或日志压缩时</li>
<li>需要内置校验和验证数据完整性时</li>
</ul>
</li>
<li>
<p><strong>优先选Deflater的场景</strong>:</p>
<ul>
<li>追求极限压缩率时</li>
<li>进行网络传输(尤其对延迟敏感时)</li>
<li>需要自定义协议格式时</li>
</ul>
</li>
<li>
<p><strong>通用原则</strong>:</p>
<ul>
<li>短文本(&lt;1KB)建议直接存储</li>
<li>中等长度文本(1KB-1MB)优先GZIP</li>
<li>大文本(&gt;1MB)可结合缓冲流处理</li>
</ul>
</li>
</ol>
<p>实际测试显示,对于典型英文文本,Deflater比GZIP压缩率高约2-5%,但解压速度慢约10-15%。</p><br><br>
来源:https://www.cnblogs.com/sailCoding/p/18921716
頁: [1]
查看完整版本: Java中Deflater和GZIP的压缩/解压实现