Java:如何恢复用python压缩的字符串数据';s zlib编码器
我通过套接字将数据从python发送到java 因此,在python 2.7方面,我有:Java:如何恢复用python压缩的字符串数据';s zlib编码器,java,python,compression,zlib,Java,Python,Compression,Zlib,我通过套接字将数据从python发送到java 因此,在python 2.7方面,我有: s = "this is test str" compressed = s.encode('zlib') push_to_tcp_socket(compressed) 所以我需要在java端恢复初始字符串。我该怎么做呢?您需要发送G字符串的长度,或者关闭连接以便知道最后一个字节在哪里 最有可能帮助您的类是DeflatorInputStream,您可以在读取字节后使用它。这是zlib类的裸包装。我还没有测试
s = "this is test str"
compressed = s.encode('zlib')
push_to_tcp_socket(compressed)
所以我需要在java端恢复初始字符串。我该怎么做呢?您需要发送G字符串的长度,或者关闭连接以便知道最后一个字节在哪里 最有可能帮助您的类是DeflatorInputStream,您可以在读取字节后使用它。这是zlib类的裸包装。我还没有测试过它可以与python一起工作,但它是您最好的选择
您可以尝试其他压缩,如Snappy或LZ4,它们具有跨平台支持。我假设您已经了解Java上的网络部分。您可以使用
Inflater
类来获得类似于javadocs中的字符串
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
//Then create string in java i assumed you are using python 2 and string is ASCII
String str = new String(result,"US-ASCII")