JDK-4040920 : Catch unexpected end of ZLIB while reading from InputStream for some ZIP files.
  • Type: Bug
  • Component: tools
  • Sub-Component: jar
  • Affected Version: 2.0,1.1,1.1.2,1.2.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,solaris_9,windows_95
  • CPU: generic,x86,sparc
  • Submitted: 1997-03-24
  • Updated: 2016-02-10
  • Resolved: 1999-01-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.2.0 1.2beta4Fixed
Related Reports
Duplicate :  
Relates :  
Description
Exception only occurs for certain files in a ZIP file.
I found this out while decompressing the latest Java Tutorial (ZIP version). 
While decompressing it, 4 files throws exception:

	ui/drawing/images/MovingImage.gif
	images/HJLogo/T12.gif
	java/threads/example/RaceTest.class
	java/threads/example/BidirectionalBubbleSortAlgorithm.class

Even though the exception is thrown, I found out that the 4 files are OK. There 
seem to be no corruption at all since I compared this with the files that was
decompressed using the "unzip" program from /usr/dist. 
One of the file "MovingImage.gif" is compress in ZIP format in the attachment
"a.zip".

To reproduce this bug, get "a.zip" from attachment, compile the code below and
type "java unzip a.zip".

Error message for this bug is:

While reading: java.io.EOFException: Unexpected end of ZLIB input stream
java.io.EOFException: Unexpected end of ZLIB input stream
        at java.lang.Throwable.<init>(Compiled Code)
        at java.lang.Exception.<init>(Compiled Code)
        at java.io.IOException.<init>(Compiled Code)
        at java.io.EOFException.<init>(Compiled Code)
        at java.util.zip.InflaterInputStream.fill(Compiled Code)
        at java.util.zip.InflaterInputStream.read(Compiled Code)
        at java.io.FilterInputStream.read(Compiled Code)
        at unzip.main(Compiled Code)


==============
Source code is:

import java.lang.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;

class unzip{
    public static void main(String[] args){
        if ( args.length < 1 ){
            System.out.println("Usage: java unzip <filename>");
            System.exit(1);
        }

        String filename=args[0];

        try{
            ZipFile fis = new ZipFile(filename);
            String line;
            byte buffer[] = new byte[1024];
            int length;

            System.out.println();

            //--- Creating files ---//
            for(Enumeration e=fis.entries(); e.hasMoreElements();){
                Object o=e.nextElement();
                line=o.toString();

                ZipEntry zentry=fis.getEntry(line);

                System.out.println("Inflating "+line+"...");
                InputStream inflate=fis.getInputStream(zentry);
                RandomAccessFile outfile=new RandomAccessFile(line,"rw");

                //-- Sometimes ZLIB unexpected EOF error occur.--//
                //-- We don't want to exit because of it. --//
                try{
                    while ( (length=inflate.read(buffer)) != -1){
                    outfile.write(buffer,0,length);
                    }
                } catch(IOException ex){
                    System.out.println("While reading: " + ex);
                    ex.printStackTrace();
                }

                outfile.close();
            }

            fis.close();
        } catch(FileNotFoundException e){
            System.err.println("Decompress: " + e);
        } catch(IOException e){
            System.err.println("Decompress: " + e);
        }
    }
}
=================================================================

This problem occurs 100% repeatably on two zip files that
I have, but some other files work okay.  I will send problem zip
files at your request.  The files were created by ZipIt on
the Mac, and can be deflated okay by unzip (PD version) on Win95
and by WinZip on Win95.

The output, including the stack trace, is:

+++ zip = java.util.zip.ZipFile@1cc748
+++ zip file entry 1 = ICONS/grain22.ico
+++      7358 characters
+++ zip file entry 2 = ICONS/grain12.ico
+++      7358 characters
+++ zip file entry 3 = ICONS/grain02.ico
java.io.EOFException: Unexpected end of ZLIB input stream
        at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:156)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:119)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:94)
        at JZip.main(JZip.java:20)


Here is the source program, which simply accesses every byte of
every entry in a zip file:

--------- JZip.java ---------------------------
import java.util.zip.*;
import java.util.*;
import java.io.*;

class JZip {

    public static void main(String[] args) {
	try {
	    ZipFile zip = new ZipFile(args[0]);
	    System.out.println("+++ zip = " + zip);
	    Enumeration entries = zip.entries();
	    int n = 0;
	    while (entries.hasMoreElements()) {
		ZipEntry entry = (ZipEntry)entries.nextElement();
		++n;
		System.out.println("+++ zip file entry " + n + " = " + entry);
		InputStream input = zip.getInputStream(entry);
		int c;
		int count = 0;
		while ((c = input.read()) >= 0)
		    ++count;  // System.out.print((char)c);
		System.out.println("+++      " + count + " characters");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

}
-----------------------------
company - Radius , email - ###@###.###

Comments
Accessible attachment added
10-02-2016

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.1.6 FIXED IN: 1.2beta4 INTEGRATED IN: 1.2beta4 VERIFIED IN: 1.2fcs
14-06-2004

WORK AROUND The workaround is to never attempt to read more bytes than the entry contains. Call ZipEntry.getSize() to get the actual size of the entry, then use this value to keep track of the number of bytes remaining in the entry while reading from it. To take the previous example: import java.lang.*; import java.io.*; import java.util.*; import java.util.zip.*; class unzip{ public static void main(String[] args){ if ( args.length < 1 ){ System.out.println("Usage: java unzip <filename>"); System.exit(1); } String filename=args[0]; try{ ZipFile fis = new ZipFile(filename); String line; byte buffer[] = new byte[1024]; int length; System.out.println(); //--- Creating files ---// for(Enumeration e=fis.entries(); e.hasMoreElements();){ Object o=e.nextElement(); line=o.toString(); ZipEntry zentry=fis.getEntry(line); System.out.println("Inflating "+line+"..."); InputStream inflate=fis.getInputStream(zentry); RandomAccessFile outfile=new RandomAccessFile(line,"rw"); //-- Sometimes ZLIB unexpected EOF error occur.--// //-- We don't want to exit because of it. --// try{ int remaining = (int)zentry.getSize(); while (remaining>0 && (length=inflate.read(buffer, 0, Math.min(1024, remaining))) != -1){ outfile.write(buffer,0,length); remaining -= length; } } catch(IOException ex){ System.out.println("While reading: " + ex); ex.printStackTrace(); } outfile.close(); } fis.close(); } catch(FileNotFoundException e){ System.err.println("Decompress: " + e); } catch(IOException e){ System.err.println("Decompress: " + e); } } }
11-06-2004

EVALUATION There is a bug in ZipFile that causes InflaterInputStream to read beyond the end of the ZIP entry when attempting to read more bytes than the entry contains. -- dac This problem has been fixed in 1.2 but will probably not be fixed for 1.1.x. david.connelly@Eng 1998-04-22 Verified in jdk1.2fcsV build. ###@###.### 1998-12-08
22-04-1998