Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Object Compression in Java with Examples

  • Difficulty Level : Hard
  • Last Updated : 02 Jul, 2020

Object Compression is the process of reducing the size of the object with the help of various classes and methods. The receiver then retrieves full information by uncompressed the object which is compressed by the sender. Take a look at below figure to distinguish the size of the file before compressing and the size the file after compressing:
 

Need for Object Compression

Java object compression is very useful in the case where we need to transfer large amounts of objects through a network. Object compression is done at the sender side and uncompressed them at the other end i.e receiver side. By this, we can improve the performance and can send and receive the objects in heavy quantity between two ends.
 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

How Object Compression is done in Java

Java object compression is done using the GZIPOutputStream class (this class implements a stream filter for writing compressed data in the GZIP file format) and passes it to the ObjectOutputStream class (this class extends OutputStream and implements ObjectOutput, ObjectStreamConstants) to write the object into an external file. 

We need to extend the Serializable class because the object we are going to compress will be represented by the User object. That’s why we need to make the User object serializable.



Implementation of Object Compression in Java

 

Java




// Java Program to demonstrate
// Object Compression
  
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.zip.GZIPOutputStream;
import java.io.File;
  
public class GFG {
  
    public static void main(String args[])
    {
  
        // Creating objects of Java Class Bill
        Bill b1
            = new Bill("176BU", "Abhishek Gupta");
        Bill b2
            = new Bill("176DA", "Sushant Singh");
        FileOutputStream f = null;
  
        // Creates a GZIPOutputStream and
        // initialize it with null
        GZIPOutputStream g = null;
  
        // Creates an ObjectOutputStream and
        // initialise it with null
        // ObjectOutputStream writes to the
        // defined GZIPOutputStream.
        ObjectOutputStream o = null;
  
        // Write path of the file in the argument
        File newFile
            = new File("File.dat");
        try {
  
            // Pass the File object
            // (newFile) to the
            // FileOutputStream
            f = new FileOutputStream(newFile);
            g = new GZIPOutputStream(f);
  
            // Now pass the GZIPOutputStream object
            // to the ObjectOutputStream
            o = new ObjectOutputStream(g);
  
            // Writes the object that are going
            // to be compressed to the
            // ObjectOutputStream using
            // writeObject(objectName)
            o.writeObject(b1);
            o.writeObject(b2);
  
            // flush() API methods of
            // ObjectOutputStream.
            o.flush();
            System.out.println(
                "Process done..");
            System.out.println(
                "Objects are compressed");
        }
        catch (
            FileNotFoundException e) {
  
            // Catch Block
            System.out.println(
                e.message());
        }
        catch (IOException e) {
  
            // Catch Block
            System.out.println(
                e.message());
        }
        finally {
            try {
  
                // Using their
                // close() API methods,
                // closes both
                // the GZIPOutputStream
                // and the
                // ObjectOutputStream
                if (o != null)
                    o.close();
                if (g != null)
                    g.close();
            }
            catch (Exception ex) {
  
                // Catch block
                System.out.println(
                    ex.message());
            }
        }
    }
}
  
class Bill implements Serializable {
  
    // Declaring the private variables
    private String billno;
    private String buyerName;
  
    // Creating constructor
    // of Java Class Bill
    public Bill(
        String bill, String buyer)
    {
        this.billno = bill;
        this.buyerName = buyer;
    }
  
    // Defining methods initializing
    // variables billno and buyerName
    public String getBill()
    {
        return billno;
    }
    public void setBill(
        String billno)
    {
        this.billno = billno;
    }
    public String getBuyerName()
    {
        return buyerName;
    }
    public void setBuyerName(
        String buyer)
    {
        this.buyerName = buyer;
    }
}

Output:

Process done..
Objects are compressed.

Before Object Compression:

After Object Compression:

The size of the file can be compressed up to 70% of its initial size.

Advantages

  1. A large number of objects can be transferred over a network without any performance issue.
  2. No data is lost during the process of compression-decompression.

Disadvantage

  1. It can take time in compression and decompression if the size of the file is very large.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!