user2572969
user2572969

Reputation: 267

decompress gzip compressed data in javascript

i am compressing data in gzip format in java like this

Object obj = parser.parse(new FileReader("/home/netstorm/FinalFunnelData.json"));
JSONObject jsonObject = (JSONObject) obj;
String data = jsonObject.toJSONString();

ByteOutputStream bos = new ByteOutputStream();

GZIPOutputStream gzip = new GZIPOutputStream(bos);
byte B_ARRAY[] = new byte[1024];
gzip.write(data.trim().getBytes());
gzip.close();
// response.setHeader("Content-Type", "application/json");
//response.setHeader("Content-Encoding", "gzip");
System.out.println(bos.toString());

out.println(bos.toString());

but i am unable to decompress it on javascript side?

Upvotes: 0

Views: 701

Answers (1)

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29975

The only thing that stands out in your code is :

out.println(bos.toString());

gzip'ed data is binary, so you cannot treat it as text (toString() and println).

I'm no Java expert, but you may want to try

GZIPOutputStream gzip = new GZIPOutputStream(out);

By giving it out directly, instead of first putting it in a ByteOutputStream, it might just do the right thing.

Upvotes: 1

Related Questions