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.
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
Process done.. Objects are compressed.
The size of the file can be compressed up to 70% of its initial size.
- A large number of objects can be transferred over a network without any performance issue.
- No data is lost during the process of compression-decompression.
- It can take time in compression and decompression if the size of the file is very large.
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.