Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何恢复用python压缩的字符串数据';s zlib编码器_Java_Python_Compression_Zlib - Fatal编程技术网

Java:如何恢复用python压缩的字符串数据';s zlib编码器

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类的裸包装。我还没有测试

我通过套接字将数据从python发送到java

因此,在python 2.7方面,我有:

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