Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

How to create a gzip file in Java? Example Tutorial

Hello guys, if you want to create a gzip file in Java but not sure how to do that then you have come to the right place. Earlier, I have shared how to create zip file in Java and in this article, I am going to share how to create a gzip file in Java. Just like we used ZipOutputStream and ZipInputStream in Java to create and read zip files in Java, you can use GZipOutputStream to create a gzip file in Java. The GZIPOutputStream class is a FilterOutputStream that compresses the data written to it using the gzip format. Now let's see an example of this. 



How to create a gzip file in Java? Example

Here's an example that shows how to create a gzip file in Java:

import java.io.*;

import java.util.zip.GZIPOutputStream;

public class GzipFileExample {

    public static void main(String[] args) throws IOException {

        String fileName = "data.txt";
        String gzipFileName = "data.txt.gz";


        // Open the original file for reading
        FileInputStream fis = new FileInputStream(fileName);

        // Wrap the original file in a BufferedInputStream for efficient reading
        BufferedInputStream bis = new BufferedInputStream(fis);


        // Open the gzip file for writing
        FileOutputStream fos = new FileOutputStream(gzipFileName);

        // Wrap the gzip file in a GZIPOutputStream for efficient compression
        GZIPOutputStream gzos = new GZIPOutputStream(fos);


        // Read from the original file and write to the gzip file

        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) > 0) {
            gzos.write(buffer, 0, len);
        }

        // Close the streams
        gzos.close();
        bis.close();

    }

}

In the code above, a file named data.txt is read, and its contents are written to a gzip file named  data.txt.gz. The FileInputStream and FileOutputStream classes are used to open the original file and
the gzip file, respectively. 

The BufferedInputStream and GZIPOutputStream classes are used  to efficiently read from the original file and write to the gzip file, respectively.

The contents of the original file are read chunks using the read method of the BufferedInputStream, and each chunk is compressed and written to the gzip file using the write method of the GZIPOutputStream. 

When all the data has been read and written, the streams are closed using the close method. This example demonstrates how to create a gzip file in Java but it can also be used  as a starting point for more advanced scenarios, such as compressing multiple files into a single gzip file, or compressing data in memory without writing to a file.

How to create a gzip file in Java? Example Tutorial




GZIPInputStream Class Example in Java

To decompress a gzip file in Java, you can use the GZIPInputStream class. The GZIPInputStream class is a FilterInputStream that decompresses the data read from it using the gzip format.

Here's an example that shows how to decompress a gzip file in Java:

import java.io.*;

import java.util.zip.GZIPInputStream;

public class UnGzipFileExample {

    public static void main(String[] args) throws IOException {

        String gzipFileName = "data.txt.gz";
        String decompressedFileName = "data.txt";



        // Open the gzip file for reading
        FileInputStream fis = new FileInputStream(gzipFileName);

        // Wrap the gzip file in a GZIPInputStream for efficient decompression
        GZIPInputStream gzis = new GZIPInputStream(fis);


        // Open the decompressed file for writing
        FileOutputStream fos = new FileOutputStream(decompressedFileName);


        // Read from the gzip file and write to the decompressed file
        byte[] buffer = new byte[1024];
        int len;
        while ((len = gzis.read(buffer)) > 0) {
           fos.write(buffer, 0, len);
        }


        // Close the streams
        fos.close();
        gzis.close();

    }

}




In the code above, a gzip file named data.txt.gz is decompressed, and its contents are written to a file named data.txt. The FileInputStream and FileOutputStream classes are used to open the gzip  file and the decompressed file, respectively. 

The GZIPInputStream class is used to efficiently decompress the gzip file. The contents of the gzip file are decompressed in chunks using  the read method of the GZIPInputStream, and each chunk is written to the decompressed file using  the write method of the FileOutputStream. When all the data has been decompressed and written, the streams are closed using the close method.


This example demonstrates how to decompress a gzip file in Java, but it can also be used as a starting point for more advanced scenarios, such as decompressing multiple gzip files into separate files, or decompressing data in memory without writing to a file.



Decompress a gzip file

It is important to note that the gzip file format is a widely used compression format and is supported by many software applications and platforms, including Linux, Windows, and macOS. Java provides a built-in library for working with gzip files, making it easy to read and write gzip files in Java.

The gzip file format is based on the DEFLATE compression algorithm, which is a lossless compression algorithm that compresses data by removing redundant information. 

The gzip file format also includes a header that contains information about the file, such as its original name and the time it was last modified. When creating gzip files in Java, it is important to choose an appropriate buffer size when reading from or writing to the gzip file. 

A buffer size that is too small may result in slow performance, while a buffer size that is too large may waste memory. 

A buffer size of 1024 bytes is commonly used, as shown in the example, but you can adjust the buffer size based on the specific requirements of your application.

It is also important to close the streams after you have finished reading from or writing to the gzip file. Failure to close the streams can result in data loss, as the data may still be buffered in memory.

The close method of the GZIPOutputStream and GZIPInputStream classes ensures that all data is flushed to disk and that the streams are closed properly.


Compression

It is also possible to set the compression level when creating gzip files in Java. The compression  level determines how aggressively the data is compressed, with higher compression levels resulting in smaller file sizes but also taking longer to compress and decompress the data.

To set the compression level, you can use the GZIPOutputStream class's constructor and pass in a Deflater object that has its compression level set. The Deflater class provides several predefined constants for the compression level, including Deflater.BEST_SPEED,

Deflater.BEST_COMPRESSION, and Deflater.DEFAULT_COMPRESSION.


Example:

Here is an example of how to create a gzip file with a specific compression level:

import java.io.*;

import java.util.zip.*;

public class GzipExample {

    public static void main(String[] args) {

        String filePath = "example.txt";
        String gzipFilePath = "example.txt.gz";

        int compressionLevel = Deflater.BEST_COMPRESSION;


        try {

            // Create a file input stream
            FileInputStream fis = new FileInputStream(filePath);


            // Create a gzip output stream with the specified compression level
            FileOutputStream fos = new FileOutputStream(gzipFilePath);
            Deflater deflater = new Deflater(compressionLevel);
            GZIPOutputStream gzipOS = new GZIPOutputStream(fos, deflater);


            // Copy the input stream to the gzip output stream

            byte[] buffer = new byte[1024];

            int len;

            while ((len = fis.read(buffer)) != -1) {

                gzipOS.write(buffer, 0, len);

            }


            // Close the streams

            gzipOS.close();

            fis.close();

        } catch (IOException e) {

            System.out.println("Error: " + e);

        }

    }

}



In this example, we set the compression level to Deflater.BEST_COMPRESSION to get the best compression possible. The example creates a gzip file from the input text file, example.txt, and saves it as example.txt.gz. 

The example uses a buffer of 1024 bytes to read and write the data, but you can adjust the buffer size based on your specific requirements.

It's important to note that decompressing gzip files with a different compression level than the one used when compressing the file will not affect the contents of the decompressed data.

The decompressed data will be the same regardless of the compression level used when compressing
the file.


Creating gzip file in Java

In addition to creating a gzip file from a text file, you can also create a gzip file from any type of  binary data. The process is similar, with the only difference being the source of the input data.


Example:

Here's an example of how to create a gzip file from binary data:

import java.io.*;

import java.util.zip.*;




public class GzipBinaryExample {

    public static void main(String[] args) {

        byte[] binaryData = new byte[] {
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10
        };

        String gzipFilePath = "binary_data.gz";

        int compressionLevel = Deflater.BEST_COMPRESSION;



        try {

            // Create a gzip output stream with the specified compression level

            FileOutputStream fos = new FileOutputStream(gzipFilePath);

            Deflater deflater = new Deflater(compressionLevel);

            GZIPOutputStream gzipOS = new GZIPOutputStream(fos, deflater);



            // Write the binary data to the gzip output stream

            gzipOS.write(binaryData);


            // Close the stream

            gzipOS.close();

        } catch (IOException e) {

            System.out.println("Error: " + e);

        }

    }

}



In this example, the binary data is represented by an array of byte values. The example creates a  gzip file from the binary data and saves it as binary_data.gz. The compression level is set to Deflater.BEST_COMPRESSION. 

When creating gzip files, it's important to properly  handle exceptions that might occur, such as FileNotFoundException or IOException. You should always close the output stream after you have finished writing data to it, to ensure that all data is flushed and the file is properly closed.


Conclusion
In conclusion, Java provides a convenient and easy-to-use library for working with gzip files. By using the GZIPOutputStream and GZIPInputStream classes, you can compress and decompress gzip files in Java, making it a powerful tool for data compression and archiving.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.