Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read from GZIPInputstream

Tags:

java

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?

like image 1000
Aajan Avatar asked Mar 04 '16 06:03

Aajan


People also ask

How to read from GZIPInputStream in Java?

Decode bytes from an InputStream, you can use an InputStreamReader. A BufferedReader will allow you to read your stream line by line. Assuming the gzipped content is text, and not binary data. The content is text only.

How to create a GZIPInputStream in Java?

Creating a GZIPInputStream InputStream fileInputStream = new FileInputStream("myfile. zip"); GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);

What is gzipinputstream in Java?

java.util.zip.GZIPInputStream.read() Method Example Advertisements Previous Page Next Page Description The java.util.zip.GZIPInputStream.read(byte[] buf, int off, int len)method reads uncompressed data into an array of bytes.

How do I read uncompressed data from a gzip input stream?

Note: The java.util.zip.GZIPInputStream.read (byte [] buf, int off, int len) method reads uncompressed data into an array of bytes. If len is not zero, the method will block until some input can be decompressed; otherwise, no bytes are read and 0 is returned.

How do I decompress a gzip file in Java?

The Java GZIPInputStream class ( java.util.zip.GZIPInputStream) can be used to decompress files that are compressed with the GZIP compression algorithm, for instance via the GZIPOutputStream class. To use the Java GZIPInputStream you must first create a GZIPInputStream instance.

How to read the contents of a zip file using zipinputstream?

The ZipInputStream's getNextEntry () reads the next ZIP file entry and positions the stream at the beginning of the entry data. The following example reads the contents of a ZIP file. The example reads the given ZIP file with ZipInputStream and prints its contents to the terminal. We print the file names, their size, and the last modification time.


2 Answers

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();
like image 172
Kordi Avatar answered Sep 19 '22 18:09

Kordi


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

like image 45
Evgeny Lebedev Avatar answered Sep 20 '22 18:09

Evgeny Lebedev