Java: how to recover string data compressed using python zlib encoder

I am sending data through a socket from python to java.

So, on the python 2.7 side, I have:

s = "this is test str" compressed = s.encode('zlib') push_to_tcp_socket(compressed) 

Therefore, I need to restore the original string on the java side. How could I do this?

+5
source share
2 answers

You will need to send the length of the gthe string or close the connection so that you know where the last byte is located.

The most likely class that will help you is DeflatorInputStream, which will be used after the bytes are read. This is a clean wrapper for the zlib class. I have not tested it with python, but this is the best option.

You can try other compressions, such as Snappy or LZ4, which support cross-platform.

0
source

I assumed that you already know the network part in Java. You can use the Inflater class to get your string, as in 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") 
0
source

Source: https://habr.com/ru/post/1232438/


All Articles