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?
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.
Creating a GZIPInputStream InputStream fileInputStream = new FileInputStream("myfile. zip"); GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
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.
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.
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.
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.
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With