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'}]

How do I compress Java objects?

This example demonstrates how to compress a Java object. We compress the Java object using GZIPOutputStream, pass it to ObjectOutputStream to write the object into an external file. The object we are going to compress will be represented by the User object. We need to make the User object serializable so it must implements the java.io.Serializable interface. You can see the complete code snippet below.

package org.kodejava.example.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPOutputStream;

public class ZipObjectDemo {
    public static void main(String[] args) {
        User admin = new User();
        admin.setId(1L);
        admin.setUsername("admin");
        admin.setPassword("secret");
        admin.setFirstName("System");
        admin.setLastName("Administrator");

        User foo = new User();
        foo.setId(2L);
        foo.setUsername("foo");
        foo.setPassword("secret");
        foo.setFirstName("Foo");
        foo.setLastName("Bar");

        System.out.println("Zipping....");
        System.out.println(admin);
        System.out.println(foo);
        try {
            FileOutputStream fos = new FileOutputStream(new File("user.dat"));
            GZIPOutputStream gos = new GZIPOutputStream(fos);
            ObjectOutputStream oos = new ObjectOutputStream(gos);

            oos.writeObject(admin);
            oos.writeObject(foo);

            oos.flush();
            oos.close();

            gos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package org.kodejava.example.util;

import java.io.Serializable;

public class User implements Serializable {
    private Long id;
    private String username;
    private String password;
    private String firstName;
    private String lastName;

    public User() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

Running the code snippet will give us the following output:

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

How do I read a zip file checksum value?

package org.kodejava.example.util;

import java.io.*;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.CheckedInputStream;
import java.util.zip.Adler32;

public class UnzipWithChecksum {
    public static void main(String[] args) {
        String zipname = "data-1.zip";

        try {
            FileInputStream fis = new FileInputStream(zipname);

            //
            // Creating input stream that also maintains the checksum of the
            // data which later can be used to validate data integrity.
            //
            CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
            ZipEntry entry;

            //
            // Read each entry from the ZipInputStream until no more entry found
            // indicated by a null return value of the getNextEntry() method.
            //
            while ((entry = zis.getNextEntry()) != null) {
                System.out.println("Unzipping: " + entry.getName());

                int size;
                byte[] buffer = new byte[2048];

                FileOutputStream fos = new FileOutputStream(entry.getName());
                BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

                while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
                    bos.write(buffer, 0, size);
                }
                bos.flush();
                bos.close();
            }

            zis.close();
            fis.close();

            //
            // Print out the checksum value
            //
            System.out.println("Checksum = " + checksum.getChecksum().getValue());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}