Open In App

How to Read Data From GZIPInputStream in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

java.lang.Object
    java.io.InputStream
        java.io.FilterInputStream
            java.util.zip.InflaterInputStream
                java.util.zip.GZIPInputStream

All Implemented Interfaces:

Closeable, AutoCloseable

public class GZIPInputStream
extends InflaterInputStream

This class implements a stream filter for reading compressed data in the GZIP file format.

Constructors of this class are as follows:

  • GZIPInputStream(InputStream in): Creates a new input stream with a default buffer size.
  • GZIPInputStream(InputStream in, int size): Creates a new input stream with the specified buffer size.

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.

Methods of this class are as follows:

Method 1: close()

Closes this input stream and releases any system resources associated with the stream

Return type: void 

Method 2: read()

Reads uncompressed data into an array of bytes

Parameters: 

  • Byte array
  • int off
  • int length

Return Type: Integer 

Now let us come onto our eccentric method listed below as follows:

Method 3: GZIPInputStream.read(byte[] buf, int off, int len) method.

Parameters:

  • byte[] buf
  • int off
  • int len

Syntax:

public int read()
throws IOException

Parameters

  • The buffer into which the data is read.
  • The start offset in the destination array b.
  • The maximum number of bytes read.

Return Type: The actual number of bytes read, or -1 if the end of the stream is reached.

Exceptions thrown:

  • NullPointerException − If buf (buffer) is null.
  • IndexOutOfBoundsException − If off is negative, len is negative, or len is greater than buf.length – off.
  • ZipException − if the compressed input data is corrupt.
  • IOException − if an I/O error has occurred.

Example

Java




// Java Program to Usage of GZIPInputStream
// via Showcasing Reading Data
 
// Importing required classes
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
 
// Main class
// GZIPInputStreamDemo
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws DataFormatException, IOException
    {
        // Custom input string  
        String message = "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;"
                         + "Welcome to Geeksforgeeks;";
 
        // Print and display the message
        System.out.println("Original Message length : "
                           + message.length());
       
        byte[] input = message.getBytes("UTF-8");
 
        // Compress the bytes
        ByteArrayOutputStream arrayOutputStream
            = new ByteArrayOutputStream();
        GZIPOutputStream outputStream
            = new GZIPOutputStream(arrayOutputStream);
        outputStream.write(input);
        outputStream.close();
 
        // Read and decompress the data
        byte[] readBuffer = new byte[5000];
        ByteArrayInputStream arrayInputStream
            = new ByteArrayInputStream(
                arrayOutputStream.toByteArray());
        GZIPInputStream inputStream
            = new GZIPInputStream(arrayInputStream);
        int read = inputStream.read(readBuffer, 0,
                                    readBuffer.length);
        inputStream.close();
        // Should hold the original (reconstructed) data
        byte[] result = Arrays.copyOf(readBuffer, read);
 
        // Decode the bytes into a String
        message = new String(result, "UTF-8");
 
        System.out.println("UnCompressed Message length : "
                           + message.length());
    }
}


Output

Original Message length : 250
UnCompressed Message length : 250


Last Updated : 18 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads