COM pressing使用GZIPOutputStream字符串字符串、COM、pressing、GZIPOutputStream

由网友(◆ ゞ。箜絔╰つ)分享简介:我要压缩我的字符串值。这些字符串值应该是相同的.NET压缩的字符串。I want to zip my string values. These string values should be same as .net zipped strings.我写的 DECOM preSS 的方法,当我发送一个.NET压缩字符...

我要压缩我的字符串值。这些字符串值应该是相同的.NET压缩的字符串。

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

我写的 DECOM preSS 的方法,当我发送一个.NET压缩字符串给它,它工作正常。但是,的的COM preSS 的方法不能正常工作。

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.

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");
}

-

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);
}

有关当我发送的例子"BQAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyLmeVlW/w+GphA2BQAAAA=="到的 DECOM preSS 的方法,它返回字符串Hello,但是当我发送的你好到的COM preSS 的方法它返回的H4sIAAAAAAAAAMtIzcnJBwCGphA2BQAAAA ==

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==".

有什么问题的的COM preSS 的方法????

What is the problem in Compress method????

推荐答案

检查使用邮编流和Base64的恩codeR对COM preSS大字符串数据

有关如何使用GZIPOutputStream / GZIInputStream和Base64的恩codeR和德codeR到COM preSS和uncom preSS大型字符串数据,因此它可以通过在HTTP响应文本。

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);
}

或者我们可以使用使用邮编流和Base64的恩codeR对COM preSS大字符串数据以避免负载整串到内存中。

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;
}
阅读全文

相关推荐

最新文章