|
|
Program: How to compress and store objects using zip utility?
Description: |
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.
|
Code: |
package com.java2novice.zip;
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;
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);
oos.writeObject(m1);
oos.writeObject(m2);
oos.flush();
System.out.println("Done... Objects are compressed and stored");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try{
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;
}
}
|
|
Output: |
Done... Objects are compressed and stored
|
|
|
|
|
List of all java.util.zip class sample examples:- How to compress byte array in java?
- How to decompress byte array in java?
- How to zip a single file?
- How to zip multiple files?
- How to read zip files entries or file name list?
- How to unzip files in java?
- How to generate checksum value for for a file in java?
- How to compress and store objects using zip utility?
- How to decompress the compressed objects using zip utility?
- How to zip a file using ZipFile class?
- How to compress a file in GZip format?
- How to uncompress a file from GZip format?
|
|
|
Preemptive scheduling Vs Time slicing?
Preemptive scheduling: The highest priority task executes until it enters
the waiting or dead states or a higher priority task comes into existence.
Time slicing: A task executes for a predefined slice of time and then reenters
the pool of ready tasks. The scheduler then determines which task should execute
next, based on priority and other factors.
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein
|