18

Scenario is to read a gzip file(.gz extension)

Got to know that there is GZIPInputStream class to handle this.

Here is the code to convert file object to GZIPStream.

FileInputStream fin = new FileInputStream(FILENAME);
 GZIPInputStream gzis = new GZIPInputStream(fin);

Doubt is how to read content from this 'gzis' object?

Aajan
  • 927
  • 1
  • 10
  • 23
  • 4
    I am confused, that is just an InputStream, you read it like any other InputStream. – njzk2 Mar 04 '16 at 06:19
  • 1
    you may be confusing zip and gzip, though. – njzk2 Mar 04 '16 at 06:20
  • Yeah its gzip not zip.I updated. – Aajan Mar 04 '16 at 06:22
  • 1
    You read the GZIPInputStream in exactly the same way you'd read the FileInputStream if the data had not been GZipped. If binary, you read into byte arrays. If text, you wrap with an InputStreamReader, specifying the character encoding. – Andreas Mar 04 '16 at 06:24
  • http://stackoverflow.com/questions/1080381/gzipinputstream-reading-line-by-line – matt Mar 04 '16 at 06:49

2 Answers2

22

Decode bytes from an InputStream, you can use an InputStreamReader. A BufferedReader will allow you to read your stream line by line.

If the zip is a TextFile

ByteArrayInputStream bais = new ByteArrayInputStream(responseBytes);
GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader in = new BufferedReader(reader);

String readed;
while ((readed = in.readLine()) != null) {
  System.out.println(readed);
}

As noticed in the comments. It will ignore the encoding, and perhaps not work always properly.

Better Solution

It will write the uncompressed data to the destinationPath

FileInputStream fis = new FileInputStream(sourcePath);
FileOutputStream fos = new FileOutputStream(destinationPath);
GZIPInputStream gzis = new GZIPInputStream(fis);
byte[] buffer = new byte[1024];
int len = 0;

while ((len = gzis.read(buffer)) > 0) {
    fos.write(buffer, 0, len);
}

fos.close();
fis.close();
gzis.close();
Kordi
  • 2,405
  • 1
  • 14
  • 13
2

I recommended you to use Apache Commons Compress API

add Maven dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.10</version>
</dependency>

then use GZipCompressorInputStream class, example described here

Eugene Lebedev
  • 1,400
  • 1
  • 18
  • 30