5

I send data through the 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)

So I need to restore initial string on the java side. How I could do that?

2 Answers 2

0

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

The most likely class to help you is the DeflatorInputStream which youc an use once the bytes have been read. This is a bare wrapper for the zlib class. I haven't tested it works with python but it's you best option.

You can try other compressions like Snappy or LZ4 which have cross platform support.

Sign up to request clarification or add additional context in comments.

Comments

0

I assumed you already know the networking part on Java. You can use Inflater class to get your string like 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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.