Publish Your Article Here

Program: How to compress and store objects using zip utility?


Sometimes we need to transfer large amount of objects over the network. We can improve the performance by compressing the objects before sending them across the network and uncompress them at the another end. Below example gives code for how to compress objects and store them to a file. Make sure that the class which is going to be compressed should implement Serializable interface.



public class MyObjectStore {

	public static void main(String a[]){
		Mobile m1 = new Mobile("1234566778", "JOCK");
		Mobile m2 = new Mobile("7686291729", "Mike");
		FileOutputStream fos = null;
		GZIPOutputStream gos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream("C:/MyObjectStore");
			gos = new GZIPOutputStream(fos);
			oos = new ObjectOutputStream(gos);
			System.out.println("Done... Objects are compressed and stored");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} finally{
				if(oos != null) oos.close();
				if(fos != null) fos.close();
			} catch(Exception ex){

class Mobile implements Serializable{
	private String number;
	private String owner;
	public Mobile(String num, String own){
		this.number = num;
		this.owner = own;
	public String getNumber() {
		return number;
	public void setNumber(String number) {
		this.number = number;
	public String getOwner() {
		return owner;
	public void setOwner(String owner) {
		this.owner = owner;

Done... Objects are compressed and stored
<< Previous Program | Next Program >>

List of all class sample examples:

  1. How to compress byte array in java?
  2. How to decompress byte array in java?
  3. How to zip a single file?
  4. How to zip multiple files?
  5. How to read zip files entries or file name list?
  6. How to unzip files in java?
  7. How to generate checksum value for for a file in java?
  8. How to compress and store objects using zip utility?
  9. How to decompress the compressed objects using zip utility?
  10. How to zip a file using ZipFile class?
  11. How to compress a file in GZip format?
  12. How to uncompress a file from GZip format?
Knowledge Centre
Inner class and Anonymous class
Inner class: classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.

Anonymous class: Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.