小编Sea*_*ean的帖子

在Python中压缩字符串以及如何在java中解压缩此字符串

在我的服务器端,我使用zlib python库压缩(zlib.compress())一个字符串,并将它插入redis.在我的redis中,它显示:

x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]
Run Code Online (Sandbox Code Playgroud)

如果我从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);
Run Code Online (Sandbox Code Playgroud)

Java会抛出错误:

java.util.zip.DataFormatException: incorrect header check
Run Code Online (Sandbox Code Playgroud)

我应该怎么解压缩呢?从其他帖子中,我发现人们正在使用GZIPInputStream.有任何性能差异吗?

java zlib

0
推荐指数
1
解决办法
7804
查看次数

标签 统计

java ×1

zlib ×1