?
Solved

Unexpected end of ZLIB input stream error

Posted on 2006-03-28
28
Medium Priority
?
1,557 Views
Last Modified: 2008-01-09
Hi:

My program is throwing "java.io.EOFException: Unexpected end of ZLIB input stream" error every now and then. The tricky part of this is that it's not reproduceable. I schedule the program to run every night. When I test the program next morning the exception is thrown it usually runs fine. Please help.

My code:

URL zipurl = new URL(myurl);
ZipInputStream zin = new ZipInputStream(zipurl.openStream());
ZipEntry ze;
   
while ((ze = zin.getNextEntry())!=null) {                                  //exception is thrown at here
      if (ze.getName().startsWith(someString)){
          InputStream in = new BufferedInputStream(zin);
          Reader isr = new InputStreamReader(in);
          BufferedReader br = new BufferedReader(isr);
   
          while ((line =br.readLine())!=null && line.trim().length()!= 0) {

           ...

           }

       }

}
0
Comment
Question by:sdc248
  • 11
  • 8
  • 5
  • +2
28 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16315867
Am i getting deja vu (with you sdc248)?
0
 

Author Comment

by:sdc248
ID: 16316083
yes, I thought the: line.trim().length()!=0
in the nested while loop had resolved the problem. It didn't. :<
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16316222
I've a feeling this could be tricky. Can you please post the stack trace?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:x4u
ID: 16316433
Maybe the zipfile itself was truncated for some reason.

>> URL zipurl = new URL(myurl);

What kind of url is this? Are you sure that it can be read entirely in all cases?
0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 16316619
Don't use a BufferedReader, and instead read from the input stream directecty ensuring you don't read more bytes than are in the ZipEntry.
And write the bytes to a ByteArrayOutputStream to get the bytes, you can then use a BufferedReader on the array of bytes read to read your uncompressed text data.
0
 
LVL 92

Expert Comment

by:objects
ID: 16316627
so two steps:
1. Read zip entry into byte array
2. Process the byte array as requried

Let me know if you have any questions.
0
 
LVL 11

Expert Comment

by:x4u
ID: 16316653
>> ensuring you don't read more bytes than are in the ZipEntry.

ZipInputStream takes care of this already
0
 
LVL 92

Expert Comment

by:objects
ID: 16316781
Not in earlier versions, what version are you running it against?
Another thing to try would be to upgrade your JRE.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16319203
>>Don't use a BufferedReader

That's certainly good advice if not all entries are text files - is that the case?

>>and instead read from the input stream directecty ensuring you don't read more bytes than are in the ZipEntry.

How would reading from the stream directly make any difference?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16319220
Try reading exactly entry size bytes from the entry
0
 
LVL 92

Expert Comment

by:objects
ID: 16319232
> Try reading exactly entry size bytes from the entry

please don't repeat previous posts, it just confuses things.
0
 
LVL 92

Expert Comment

by:objects
ID: 16319237
> That's certainly good advice if not all entries are text files - is that the case?

and the type of file is irrelevent
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16319262
>>please don't repeat previous posts, it just confuses things.

LOL. Which previous post was that exactly? If i had done, it wouldn't 'confuse' things - it would reinforce them
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 300 total points
ID: 16319271
while ((ze = zin.getNextEntry())!=null && bytesRead <= ze.getSize())            
0
 
LVL 92

Expert Comment

by:objects
ID: 16319298
> ensuring you don't read more bytes than are in the ZipEntry.

helps to read previous posts, in fact you are expected to.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16319345
>>helps to read previous posts, in fact you are expected to.

I've not only read it, but commented on that piece of 'advice'. I notice you haven't answered my question about it ...
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16319380
Hi sdc248,

You need to call the closeEntry() method:

while ((ze = zin.getNextEntry())!=null) {                                  //exception is thrown at here
      if (ze.getName().startsWith(someString)){
          InputStream in = new BufferedInputStream(zin);
          Reader isr = new InputStreamReader(in);
          BufferedReader br = new BufferedReader(isr);
   
          while ((line =br.readLine())!=null && line.trim().length()!= 0) {

           ...

           }
           zin.closeEntry(); // ready to read next entry
       }

}
0
 

Author Comment

by:sdc248
ID: 16324179
Hi guys, thanks for the input. Regarding your questions:

1. stack trace: I didn't take it (damn!)
2. the url and the zip file both look ok, although I don't know what kind of zip program the file publisher uses. I read from somewhere that Jave (old version, maybe) has bug while dealing with zip file generated by certain zip program and that it tries to read beyond the input stream.
3. my jdk/jre version is 1.5.0
4. the zip file in question contains about 30 files in it. They are csv files. I am not sure how a csv file differs from a text file, except that there are commas in between tokens.
5. my program has to process the content of the csv file, line by line. For example, for every line, first token is the date, 2nd token is the location, 3rd to 27 are the prices from hour 1 to hour 24 for that day. Sometimes part of the prices could be missing. By using BufferedInputStream I could read one line, parse it, process token by token, if anything worng, throw exception, if not next line. It's easy and safe to me. objects advice might work, but just a little unclear to me. For one thing, how many bytes should I read from the byte array each time? Plus, now that the exception is thrown at the code that is getting zip entries (it hasn't started to read the file yet), I wonder whether that is the solution?



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16324237
>>They are csv files. I am not sure how a csv file differs from a text file

They are text files

Did you try Webstorm's suggestion?
0
 

Author Comment

by:sdc248
ID: 16324839
i certainly have implmented Webstorm's suggestion. But I am not sure whether this is the solution since the exception was thrown even before the if block starts...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16324868
On the first pass through the loop?
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 300 total points
ID: 16325684
>> But I am not sure whether this is the solution since the exception was thrown even before the if block starts...

Then you can also try to put   zin.closeEntry();   outside the if, just before the end of the while loop.

But as you get it the file from network, you may get wrong zip file. Try to save it to a file before reading this file to test if it's a real zip file. In case you got wrong data from network, you may catch the exception and retry to download it again.
0
 
LVL 92

Expert Comment

by:objects
ID: 16327176
> For one thing, how many bytes should I read from the byte array each time

as many as u want as long as its less than the amout remaining in the entry.
Just keep track of the amount read from each entry, that way you know how many bytes are remaining.
0
 

Author Comment

by:sdc248
ID: 16327204
probably not the first pass as I remember there was a pause (of those outputs of my System.out.println()s)  before the error was thrown.

Copying the zip file is probably a good idea. I could use it to re-produce the problem for later testing.

Will put another zin.closeEntry() as suggested by Webstorm. Guess have to wait for a few weeks to see whether it really works.
0
 
LVL 92

Expert Comment

by:objects
ID: 16327229
> Copying the zip file is probably a good idea. I could use it to re-produce the problem for later testing.

And you could delete the copy on success
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16327298
>>For one thing, how many bytes should I read from the byte array each time?

See http:Q_21792744.html#16319271
0
 

Author Comment

by:sdc248
ID: 16433216
Hi:

Sorry for belating reply but I got tied up with the other projects. Since I don't know when I could come back to this issue I am closing the thread for now and spliting the points equally to you guys. Thank you very much for the efforts to help. I will open a new thread if I have further questions.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16438462
:-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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