zip

Compress Objects example

With this example we are going to demonstrate how to compress and expand an Object. We have implemented the ObjectCompressionUtil Class, that extends the Serializable class and has a parameter that is the generic type of the serializable object to be compressed. The class consists of two methods.

    The compressObject(final T objectToCompress, final OutputStream outstream) takes the object to compress and an OutputStream and returns the compressed object, as described below:

  • Creates a new GZIPOutputStream with a default buffer size.
  • Creates an ObjectOutputStream that writes to the specified GZIPOutputStream. 
  • Writes the specified object to be compressed to the ObjectOutputStream and flush it, using writeObject(Object obj) and flush() API methods of ObjectOutputStream.
  • Closes both the GZIPOutputStream and the ObjectOutputStream, using their close() API methods.
    The expandObject(final T objectToExpand, final InputStream instream) method takes the object to expand and an InputStream and returns the expanded object, as shown below:

  • Creates a new GZIPInputStream with a default buffer size.
  • Creates an ObjectInputStream that reads from the specified GZIPInputStream.
  • Reads the object to expand from the ObjectInputStream, with readObject() API method of ObjectInputStream.
  • Returns the expanded object and closes both the GZIPInputStream and the ObjectInputStream, using their close() API methods.

Let’s take a look at the code snippet that follows:  

package javaitzen.blog;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
 
/**
 * The Class ObjectCompressionUtil.
 * 
 * @param <T> the generic type of the serializable object to be compressed
 */
public class ObjectCompressionUtil<T extends Serializable> {
 
 /**
  * Compress object.
  * 
  * @param objectToCompress the object to compress
  * @param outstream the outstream
  * @return the compressed object
  * @throws IOException Signals that an I/O exception has occurred.
  */
 public T compressObject(final T objectToCompress, final OutputStream outstream) throws IOException {
 
  final GZIPOutputStream gz = new GZIPOutputStream(outstream);
  final ObjectOutputStream oos = new ObjectOutputStream(gz);
   
  try {
  oos.writeObject(objectToCompress);
  oos.flush();
  return objectToCompress;
  }finally {
   oos.close();
   outstream.close();
  }
 
 }
 
 /**
  * Expand object.
  * 
  * @param objectToExpand the object to expand
  * @param instream the instream
  * @return the expanded object
  * @throws IOException Signals that an I/O exception has occurred.
  * @throws ClassNotFoundException the class not found exception
  */
 public T expandObject(final T objectToExpand, final InputStream instream) throws IOException,
   ClassNotFoundException {
  final GZIPInputStream gs = new GZIPInputStream(instream);
  final ObjectInputStream ois = new ObjectInputStream(gs);
 
  try {
   @SuppressWarnings("unchecked")
   T expandedObject = (T) ois.readObject();
   return expandedObject;
  } finally {
   gs.close();
   ois.close();
  }
 }
 
}

Related Article:

Reference: Java Compression from our JCG partner Brian at Zen in the art of IT

  
This was an example of how to compress and expand an Object in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button