Solved

Compression of a seriaizable object

Posted on 2004-04-12
6
267 Views
Last Modified: 2011-04-14
I'm looking for a simple example of compressing a serializable object using the GZIPInputStream and GZIPOutputStream classes in java.util.zip package.  Any help will be greatly appreciated.
0
Comment
Question by:mwalker
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
brunomsilva earned 50 total points
ID: 10808968

to write:

FileOutputStream fos = new FileOutputStream(filename);
GZIPOutputStream gzos = new GZIPOutputStream(fos);    
ObjectOutputStream out = new ObjectOutputStream(gzos);
out.writeObject(new String("test string"));

to read:

FileInputStream fis = new FileInputStream(filename);
GZIPInputStream gzis = new GZIPInputStream(fis);
ObjectInputStream in = new ObjectInputStream(gzis);
String test = (Vector)in.readObject();  // this depends on what you want to write

don't forget to add the exception handling

cheers,
 bruno
0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 25 total points
ID: 10808987
ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(myFile)));
oos.writeObject(mySerializableObject);

ObjectInputStream ois = new ObjectInputStream(new GZIPInputStream(new FileInputStream(myFile)));
Object deserialized = ois.readObject();
0
 
LVL 3

Assisted Solution

by:int_20h
int_20h earned 25 total points
ID: 10809002
// Serialize & Zip
{
  MyObject myobj = new MyObject();
  FileOutputStream fos = new FileOutputStream("data.dat");
  GZIPOutputStream gos = new GZIPOutputStream(fos);
  ObjectOutputStream oos = new ObjectOutputStream(gos);
  oos.writeObject(myobj);
}
// Unzip & De-serialize
{
  MyObject myobj = null;
  FileInputStream fis = new FileInputStream("data.dat");
  GZIPInputStream gis = new GZIPInputStream(fis);
  ObjectInputStream ois = new ObjectInputStream(gis);
  myobj = (MyObject) ois.readObject();  
}
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10809176
int_20h, please don't post already posted information
0
 
LVL 3

Expert Comment

by:int_20h
ID: 10809249
Sorry... I was writting the information when there was no answer.  After I answer I saw all other replies!
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10809405
That's ok. ;-)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 81
Java Loop 6 60
HashTable highest marks enumeration alternative 9 37
spring jars download 1 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question