Link to home
Start Free TrialLog in
Avatar of sdc248
sdc248Flag for United States of America

asked on

Unexpected end of ZLIB input stream error

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) {

           ...

           }

       }

}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Am i getting deja vu (with you sdc248)?
Avatar of sdc248

ASKER

yes, I thought the: line.trim().length()!=0
in the nested while loop had resolved the problem. It didn't. :<
I've a feeling this could be tricky. Can you please post the stack trace?
Avatar of x4u
x4u

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?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
>> ensuring you don't read more bytes than are in the ZipEntry.

ZipInputStream takes care of this already
Not in earlier versions, what version are you running it against?
Another thing to try would be to upgrade your JRE.
>>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?
Try reading exactly entry size bytes from the entry
> Try reading exactly entry size bytes from the entry

please don't repeat previous posts, it just confuses things.
> That's certainly good advice if not all entries are text files - is that the case?

and the type of file is irrelevent
>>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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> ensuring you don't read more bytes than are in the ZipEntry.

helps to read previous posts, in fact you are expected to.
>>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 ...
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
       }

}
Avatar of sdc248

ASKER

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?



>>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?
Avatar of sdc248

ASKER

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...
On the first pass through the loop?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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.
Avatar of sdc248

ASKER

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.
> 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
>>For one thing, how many bytes should I read from the byte array each time?

See http:Q_21792744.html#16319271
Avatar of sdc248

ASKER

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.
:-)