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?

silent_coder
  • 6,222
  • 14
  • 47
  • 91

2 Answers2

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
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")
DreadfulWeather
  • 716
  • 3
  • 13