I am using Apache HttpClient to stream a gzipped file from a server. I retrieve the stream from the http response and wrap it with a GZIPInputStream and then attempt to read and print it using the following code:
I would expect the contents of the file to be written out uncompressed but this is not so - the System.out writes out the binary data contents.
I know the file is ok, because if I save the file to disk and then read it in like this:
InputStream is = getClass().getResourceAsStream("data.txt.gz");
InputStream zipStream = new GZIPInputStream ( is );
System.out.println(IOUtils.toString(zipStream));
That actualy uncompresses it.. so what is wrong with uncompressing it on the fly?
Code:
GZIPInputStream zipin = new GZIPInputStream(in);
int sChunk = 8192;
byte[] buffer = new byte[sChunk];
// decompress the file
int length;
while ((length = zipin.read(buffer, 0, sChunk)) != -1)
System.out.write(buffer, 0, length);
zipin.close();
I know the file is ok, because if I save the file to disk and then read it in like this:
InputStream is = getClass().getResourceAsStream("data.txt.gz");
InputStream zipStream = new GZIPInputStream ( is );
System.out.println(IOUtils.toString(zipStream));
That actualy uncompresses it.. so what is wrong with uncompressing it on the fly?