使用 GZIPOutputStream 壓縮字符串 (Compressing a string using GZIPOutputStream)


問題描述

使用 GZIPOutputStream 壓縮字符串 (Compressing a string using GZIPOutputStream)

I want to zip my string values. These string values should be same as .net zipped strings.

I wrote Decompress method and when I send a .net zipped string to it, it works correctly. But the Compress method does not work correctly.

<pre class="lang-java prettyprint-override">public static String Decompress(String zipText) throws IOException {
    int size = 0;
    byte[] gzipBuff = Base64.decode(zipText);

    ByteArrayInputStream memstream = new ByteArrayInputStream(gzipBuff, 4,
            gzipBuff.length - 4);
    GZIPInputStream gzin = new GZIPInputStream(memstream);

    final int buffSize = 8192;
    byte[] tempBuffer = new byte[buffSize];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((size = gzin.read(tempBuffer, 0, buffSize)) != -1) {
        baos.write(tempBuffer, 0, size);
    }
    byte[] buffer = baos.toByteArray();
    baos.close();

    return new String(buffer, "UTF-8");
}
</code></pre>

<pre class="lang-java prettyprint-override">public static String Compress(String text) throws IOException {
    byte[] gzipBuff = EncodingUtils.getBytes(text, "UTF-8");

    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    GZIPOutputStream gzin = new GZIPOutputStream(bs);

    gzin.write(gzipBuff);
    gzin.finish();
    bs.close();

    byte[] buffer = bs.toByteArray();
    gzin.close();

    return Base64.encode(buffer);
}
</code></pre>

For example when I send "BQAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyLmeVlW/w+GphA2BQAAAA==" to Decompress method It returns the string "Hello", but when I send "Hello" to Compress method It returns "H4sIAAAAAAAAAMtIzcnJBwCGphA2BQAAAA==".

What is the problem in Compress method????


參考解法

方法 1:

Check Use Zip Stream and Base64 Encoder to Compress Large String Data

about how to use GZIPOutputStream/GZIInputStream and Base64 Encoder and Decoder to compress and uncompress large string data, so it can be passed as text in http response.

public static String compressString(String srcTxt) throws IOException {
  ByteArrayOutputStream rstBao = new ByteArrayOutputStream();
  GZIPOutputStream zos = new GZIPOutputStream(rstBao);
  zos.write(srcTxt.getBytes());
  IOUtils.closeQuietly(zos);

  byte[] bytes = rstBao.toByteArray();
  return Base64.encodeBase64String(bytes);
}

Or we can use Use Zip Stream and Base64 Encoder to Compress Large String Data to avoid load whole string into memory.

public static String uncompressString(String zippedBase64Str) throws IOException {
  String result = null;
  byte[] bytes = Base64.decodeBase64(zippedBase64Str);
  GZIPInputStream zi = null;
  try {
    zi = new GZIPInputStream(new ByteArrayInputStream(bytes));
    result = IOUtils.toString(zi);
  } finally {
    IOUtils.closeQuietly(zi);
  }
    return result;
}

方法 2:

I have tried with java vm i suppose the result is the same. Use this line at the end of your Compress method :

return new String(base64.encode(buffer), "UTF-8");

(by BobsJeffersonZhangrevo)

參考文件

  1. Compressing a string using GZIPOutputStream (CC BY-SA 3.0/4.0)

#compression #string #gzipoutputstream #Android #gzip






相關問題

CSS 壓縮和組合 / js 縮小 - 在運行時或構建時更好? (CSS compression & combining / js minification - Better to do at runtime or at build time?)

在javascript中壓縮包含音頻PCM數據的blob (compress blob containing audio PCM data in javascript)

如何提高@font-face 的加載性能 (How to improve loading performance of @font-face)

ServiceStack 流壓縮 (ServiceStack Stream Compression)

有沒有辦法讀取壓縮存檔的屬性? (Is there any way to read the properties of a Compressed Archive?)

德爾福 2009 中的 Zlib (Zlib in Delphi 2009)

SQL 2008開啟頁面壓縮後如何回收空間? (How to reclaim space after turning on page compression in SQL 2008?)

本機 Lua 中的高效可變字節數組 (Efficient mutable byte array in native Lua)

在 JavaScript 中壓縮純文本? (Compressing plaintext in JavaScript?)

如何遞歸壓縮文件夾? (How to zip a folder recursively?)

使用 GZIPOutputStream 壓縮字符串 (Compressing a string using GZIPOutputStream)

壓縮後 1 KB 可以容納多少文本? (How much text I can fit in 1 kilobyte with compression?)







留言討論