0

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?

1
  • Did you ever solve this? I'm in the same predicament... Commented Apr 21, 2016 at 16:19

1 Answer 1

5

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!

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

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.