Main Tutorials

How to decompress serialized object from a Gzip file

In last section, you learn about how to compress a serialized object into a file, now you learn how to decompress it from a Gzip file.


FileInputStream fin = new FileInputStream("c:\\address.gz");
GZIPInputStream gis = new GZIPInputStream(fin);
ObjectInputStream ois = new ObjectInputStream(gis);
address = (Address) ois.readObject();

GZIP example

In this example, you will decompress a compressed file “address.gz“, and print it out the value.


package com.mkyong.io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;

public class Deserializer implements Serializable{

   public static void main (String args[]) {
    
	   Deserializer deserializer = new Deserializer();
	   Address address = deserializer.deserialzeAddress();
	   System.out.println(address);
   }

   public Address deserialzeAddress(){
	   
	   Address address;
	 
	   try{
		    
		   FileInputStream fin = new FileInputStream("c:\\address.gz");
		   GZIPInputStream gis = new GZIPInputStream(fin);
		   ObjectInputStream ois = new ObjectInputStream(gis);
		   address = (Address) ois.readObject();
		   ois.close();
		  
		   return address;
		   
	   }catch(Exception ex){
		   ex.printStackTrace();
		   return null;
	   } 
   } 
}

Output


 Street : wall street Country : united state

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments