Sean
Sean

Reputation: 87

compress a string in Python and how to decompress this string in java

On my serverside, I used zlib python library to compress (zlib.compress()) a string and insret it into redis. In my redis, it shows:

x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]

If I read it from redis to python and use python zlib.decompress(), it works. It can print "Hello World".

How can I do it in java?

I tried this piece of code from Java 7 official documents.

String temp ="x\\xda\\xcbH\\xcd\\xc9\\xc9\\x07\\x00\\x06,\\x02\\x15";
byte[] output=temp.getBytes();
System.out.println(new String(output));
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0,output.length);
byte[] result = new byte[10000];
int resultLength = decompresser.inflate(result);
decompresser.end();

// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println(outputString);

Java will throw error:

java.util.zip.DataFormatException: incorrect header check

What should I in order to decompress it? From other posts, I found people are using GZIPInputStream. Is there any performance difference?

Upvotes: 0

Views: 8469

Answers (1)

dacuna
dacuna

Reputation: 1096

Pretty late, but today I found myself working on exactly the same problem. I manage to solve it like this:

Python code (compress):

import zlib
import base64

data = "Hello World"
c_data = zlib.compress(data)
# to be able to transmit the data we need to encode it
final_data = base64.b64encode(c_data)
data_size = len(data) # we need this to decompress in java

Java code (decompress), I'm using Java 8 so we have a built in base64 decoder, for other java versions there are plenty of decoders out there. Also to keep things short, I didn't put the exception handling code:

String dataFromPython = ... //put your final_data here
byte[] decoded = Base64.getDecoder().decode(dataFromPython);
Inflater decompresser = new Inflater();
decompresser.setInput(decoded, 0, decoded.length);
byte[] result = new byte[data_size]; //remember the data_size var from python?
int resultLength = decompresser.inflate(result);
decompresser.end();
//Assumptions: python 2, ASCII string
String final_data = new String(result, "US-ASCII");
System.out.prinln(final_data); //prints "Hello World"

Hope it helps somebody!

Upvotes: 5

Related Questions