Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:08:50+00:00 2026-06-10T08:08:50+00:00

I am new to Java. I want to learn to use GZIPstreams. I already

  • 0

I am new to Java. I want to learn to use GZIPstreams. I already have tried this:

ArrayList<SubImage>myObject = new ArrayList<SubImage>(); // SubImage is a Serializable class

ObjectOutputStream compressedOutput = new ObjectOutputStream(
   new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(
   new File("....")))));
compressedOutput.writeObject(myObject);

and

ObjectInputStream compressedInput = new ObjectInputStream(
   new BufferedInputStream(new GZIPInputStream(new FileInputStream(
   new File("....")))));
myObject=(ArrayList<SubImage>)compressedInput.readObject();

When the program writes myObject to a file without throwing any exception, but when it reaches the line

myObject=(ArrayList<SubImage>)compressedInput.readObject();

it throws this exception:

Exception in thread "main" java.io.EOFException: Unexpected end of ZLIB input stream

How can I solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:08:52+00:00Added an answer on June 10, 2026 at 8:08 am

    You have to flush and close your outputstream. Otherwhise, at least, the BufferedOutputStream will not write everything to the file (it does in big chucks to avoid penalizing performance).

    If you call compressedOutput.flush() and compressedOutput.close() it will suffice.

    You can try writing a simple string object and checking if the file is well written.

    How? If you write a xxx.txt.gz file you can open it with your preferred zip app and look at the xxx.txt. If the app complains, then the content is not full written.

    Extended answer to a comment: compressing even more the data

    Changing serialization

    You could change the standard serialization of SubImage object if it’s an object of your own. Check java.io.Serializable javadoc to know how to do it. It’s pretty straightforward.

    Writing just what you need

    Serialization has the drawback that needs to write “it’s a SubImage” just before every instance you write. It’s not necessary if you know what’s going to be there beforehand. So you could try to serialize it more manually.

    To write your list, instead of writing an object write directly the values that conform your list. You will need just a DataOutputStream (but ObjectOutputStream is a DOS so you can use it anyway).

    dos.writeInt(yourList.size()); // tell how many items
    for (SubImage si: yourList) {
       // write every field, in order (this should be a method called writeSubImage :)
       dos.writeInt(...);
       dos.writeInt(...);
       ...
    }
    
    // to read the thing just:
    int size = dis.readInt();
    for (int i=0; i<size; i++) {
       // read every field, in the same order (this should be a method called readSubImage :)
       dis.readInt(...);
       dis.readInt(...);
       ...
       // create the subimage
       // add it to the list you are recreating
    }
    

    This method is more manual but if:

    1. you know what’s going to be written
    2. you will not need this kind of serialization for many types

    it’s pretty affordable and definitively more compressed than the Serializable counterpart.

    Have in mind that there are alternative frameworks to serialize objects or create string messages (XStream for xml, Google Protocol Buffers for binary messages, and so on). That frameworks could work directly to binary or writing a string that could be then written.

    If your app will need more on this, or just curious, maybe you should look at them.

    Alternative serialization frameworks

    Just looked in SO and found several questions (and answers) addressing this issue:

    https://stackoverflow.com/search?q=alternative+serialization+frameworks+java

    I’ve found that XStream is pretty easy and straightforward to use. And JSON is a format pretty readable and succint (and Javascript compatible which could be a plus :).

    I should go for:

    Object -> JSON -> OutputStreamWriter(UTF-8) -> GZippedOutputStream -> FileOutputStream
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to learn a new programming language. I have in mind stuff like
I'm new to Java and as an attempt to learn more I tried making
Wen I have this code: import java.util.Scanner; import java.util.Arrays; public class Ex02 { /**
I have no experience with Java, but I want to learn it. I have
I am new to Java Swing I want to develop an POS application which
I'm new to Java. I want to create Java Array and insert data into
I'm new to Java programming. I want to write simple Java network protocol. I
I am very new to Java, I want to add a PNG image to
greetings, I'm new with java script so bear with me! I want to achieve
Good day! I'm new in using Java DB (Derby). I want to embed it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.