How do I decompress Java objects?

Category: java.util.zip, viewed: 2885 time(s).

On 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'll read the file and reconstruct the compressed objects. For the User class you can find it also in the previous example mentioned above.

package org.kodejava.example.util;

import java.io.FileInputStream;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.IOException;
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 e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("Admin = [" + admin + "]");
        System.out.println("Foo = [" + foo + "]");
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats