The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial : Java IO (Java File - How to compress serialized object into a file - Gzip)

0 replies.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java IO (Java File - How to compress serialized object into a file - Gzip) Posted: Nov 24, 2016 10:05 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java IO (Java File - How to compress serialized object into a file - Gzip)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=dKN02FjwfvE&list=UUhwKlOVR041tngjerWxVccw

Person.java
import java.io.Serializable;

public class Person implements Serializable
{
private static final long serialVersionUID = 3069227031912694124L;
private String name;
private int age;

public Person(String name, int age)
{
super();
this.name = name;
this.age = age;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

}
GZipSerializeDemo.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPOutputStream;

/*
* We can compress the serialized object to
* reduce the file size.
*/

public class GZipSerializeDemo
{

public static void main(String[] args) throws IOException
{
GZipSerializeDemo gzipSerializeDemo = new GZipSerializeDemo();
String gzipPath = "D:/work/person.gz";
gzipSerializeDemo.gzipPersonObject(gzipPath);
}

public void gzipPersonObject(String gzipPath) throws IOException
{

Person person = new Person("Peter", 45);
/*
* If the Streams are within the
* "try-With-Resources" block, then it will be
* closed automatically.
*/

try (FileOutputStream fos = new FileOutputStream(gzipPath);
GZIPOutputStream gz = new GZIPOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(gz);)

{

oos.writeObject(person);
System.out.println("Person object is serialized and compressed.");

}

}
}
Output
Person object is serialized and compressed.

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

/*
* How to decompress serialized object from a Gzip file.
*/

public class GZipDeserializeDemo
{

public static void main(String[] args) throws IOException,
ClassNotFoundException
{
GZipDeserializeDemo gzipDeserializeDemo = new GZipDeserializeDemo();
String gzipPath = "D:/work/person.gz";
Person person = gzipDeserializeDemo.deserializePersonObject(gzipPath);
System.out.println("Name : " + person.getName());
System.out.println("Age : " + person.getAge());
}

public Person deserializePersonObject(String gzipPath)
throws ClassNotFoundException, IOException
{
/*
* If the Streams are within the
* "try-With-Resources" block, then it will be
* closed automatically.
*/

try (FileInputStream fin = new FileInputStream(gzipPath);
GZIPInputStream gis = new GZIPInputStream(fin);
ObjectInputStream ois = new ObjectInputStream(gis);)
{

Person person = (Person) ois.readObject();

return person;

}

}
}
Output
Name : Peter
Age : 45

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_Gzip_Serialize_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_Gzip_Serialize_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/97750dcb5a5d126b3956b792a4fc850ca4dd67ca/BasicJava/JavaIODemo_Gzip_Serialize_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: Java Tutorial : Java IO (Java File - How to compress serialized object into a file - Gzip)


    Topic: Java Tutorial : Java IO (Java File - How to compress a file in GZIP format) Previous Topic   Next Topic Topic: Java Bullshifier – Generate Massive Random Code Bases

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use