Solved

Compression of a seriaizable object

Posted on 2004-04-12
6
266 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse compiler vs Installed JREs option 3 77
backtracking recursion  code 19 54
Image decoding from Camera 3 72
Java SE 8u111  Lot of stuff broke 11 54
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now