【发布时间】:2015-11-29 16:30:29
【问题描述】:
在我的服务器端,我使用 zlib python 库来压缩 (zlib.compress()) 一个字符串并将其插入到 redis 中。在我的redis中,它显示:
x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]
如果我从 redis 读取到 python 并使用 python zlib.decompress(),它可以工作。它可以打印“Hello World”。
我怎样才能在java中做到这一点?
我尝试了 Java 7 官方文档中的这段代码。
String temp ="x\\xda\\xcbH\\xcd\\xc9\\xc9\\x07\\x00\\x06,\\x02\\x15";
byte[] output=temp.getBytes();
System.out.println(new String(output));
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0,output.length);
byte[] result = new byte[10000];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println(outputString);
Java 会抛出错误:
java.util.zip.DataFormatException: incorrect header check
我应该怎么解压?从其他帖子中,我发现人们正在使用 GZIPInputStream。有性能差异吗?
【问题讨论】:
-
你解决过这个问题吗?我也处于同样的困境中......