Solved

Compression of a seriaizable object

Posted on 2004-04-12
6
262 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

743 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

12 Experts available now in Live!

Get 1:1 Help Now