COM pressing使用GZIPOutputStream字符串 [英] Compressing a string using GZIPOutputStream

查看:365
本文介绍了COM pressing使用GZIPOutputStream字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要压缩我的字符串值。这些字符串值应该是相同的.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????

推荐答案

检查<一href="http://lifelongprogrammer.blogspot.com/2013/11/java-use-zip-stream-and-base64-to-com$p$pss-big-string.html"相对=nofollow>使用邮编流和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);
}

或者我们可以使用<一个href="http://lifelongprogrammer.blogspot.com/2013/11/java-use-zip-stream-and-base64-to-com$p$pss-big-string.html"相对=nofollow>使用邮编流和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;
}

这篇关于COM pressing使用GZIPOutputStream字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆