How do I compress a file in Gzip format?

In this code example we will learn how to compress a file using the gzip compression. By its nature, gzip can only compress a single file, you can not use it for compressing a directory and all the files in that directory.

Classes that you will be using to compress a file in gzip format includes the GZipOutputStream, FileInputStream and FileOutputStream classes. The steps for compressing a file described in the comments of the code snippet below.

package org.kodejava.example.zip;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class GZipCompressExample {
    public static void main(String[] args) {
        //
        // GZip input and output file.
        //
        String sourceFile = "core-sample/target/classes/input.txt";
        String targetFile = "core-sample/target/classes/output.gz";

        try {
            //
            // Create a file output stream the write the gzip result into
            // a specified file name.
            //
            FileOutputStream fos = new FileOutputStream(targetFile);

            //
            // Create a gzip output stream object with file output stream
            // as the argument.
            //
            GZIPOutputStream gzos = new GZIPOutputStream(fos);

            //
            // Define buffer and temporary variable for iterating the file
            // input stream.
            //
            byte[] buffer = new byte[1024];
            int length;

            //
            // Create a file input stream of the file that is going to be
            // compressed.
            //
            FileInputStream fis = new FileInputStream(sourceFile);

            //
            // Read all the content of the file input stream and write it
            // to the gzip output stream object.
            //
            while ((length = fis.read(buffer)) > 0) {
                gzos.write(buffer, 0, length);
            }

            //
            // Finish file compressing and close all streams.
            //
            fis.close();
            gzos.finish();
            gzos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I decompress Java objects?

In the previous example How do I compress Java objects? we have manage to compress Java objects and stored them in file. In this example we will read the file and reconstruct the compressed objects. For the User class you can see in the previous example mentioned above.

package org.kodejava.example.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.zip.GZIPInputStream;

public class UnzipObjectDemo {
    public static void main(String[] args) {
        User admin = null;
        User foo = null;

        try {
            FileInputStream fis = new FileInputStream(new File("user.dat"));
            GZIPInputStream gis = new GZIPInputStream(fis);
            ObjectInputStream ois = new ObjectInputStream(gis);

            admin = (User) ois.readObject();
            foo = (User) ois.readObject();

            ois.close();
            gis.close();
            fis.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("Admin = [" + admin + "]");
        System.out.println("Foo = [" + foo + "]");
    }
}

Running the code snippet above will give us the following output:

Admin = [User{id=1, username='admin', password='secret', firstName='System', lastName='Administrator'}]
Foo = [User{id=2, username='foo', password='secret', firstName='Foo', lastName='Bar'}]