JDK-4118091 : ZipOutputStream: java.util.zip.ZipException: invalid entry CRC-32
  • Type: Bug
  • Component: tools
  • Sub-Component: jar
  • Affected Version: 1.2.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 1998-03-09
  • Updated: 1999-01-19
  • 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
Description
I have a small program that reads entries from a zip (jar) file, and writes them
out to another zip file.

When I use our nightly build, I get an error "expected 0xffffffff8bcdbdcc but got 0x8bcdbdcc":

laguna{luehe}115: /usr/security/nightly/ws/solaris/JDK1.2bugs/build/solaris/bin/java ZipTest x509.jar out.jar
META-INF/MANIFEST.MF: method= 8, CRC=-1949450804
META-INF/TTT.SF: method= 8, CRC=-450279610
java.util.zip.ZipException: invalid entry CRC-32 (expected 0xffffffff8bcdbdcc but got 0x8bcdbdcc)
        at java.util.zip.ZipOutputStream.closeEntry(ZipOutputStream.java:195)
        at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:113)
        at ZipTest.writeEntry(ZipTest.java:75)
        at ZipTest.run(ZipTest.java:51)
        at ZipTest.main(ZipTest.java:19)
 

However, when I use the "JDK-1.2beta3-K" build, everything seems to work fine.

laguna{luehe}116: /usr/local/java/jdk1.2/solaris/bin/java ZipTest x509.jar out.jar
x509/PKIXExtensions.class: method= 8, CRC=1944216257
x509/Extension.class: method= 8, CRC=1078045011
x509/RDN.class: method= 8, CRC=3033619565
x509/DNSName.class: method= 8, CRC=335038015
x509/BasicConstraintsExtension.class: method= 8, CRC=2726800908
x509/ReasonFlags.class: method= 8, CRC=3264635430
x509/EDIPartyName.class: method= 8, CRC=3811599804
META-INF/TTT.DSA: method= 8, CRC=1197588713
x509/NameConstraintsExtension.class: method= 8, CRC=2567528146
x509/RevokedCertImpl.class: method= 8, CRC=1341248530
x509/: method= 0, CRC=0
x509/cacerts/: method= 0, CRC=0
META-INF/TTT.SF: method= 8, CRC=3844687686
META-INF/MANIFEST.MF: method= 8, CRC=2345516492
x509/cacerts/Verisign.class: method= 8, CRC=2309307208
x509/RFC822Name.class: method= 8, CRC=3372306682
x509/PrivateKeyUsageExtension.class: method= 8, CRC=214863645


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

public class ZipTest {

    ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
    byte[] buffer = new byte[8192];

    public static void main(String[] args) {

	if (args.length != 2) {
	    System.err.println("usage: java ZipTest <in> <out>");
	    System.exit(1);
	}

	try {
	    ZipTest zt = new ZipTest();
	    zt.run(args);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    void run(String[] args) throws Exception {

	// Open the jar (zip) file
	ZipFile zipFile = null;
	try {
	    zipFile = new ZipFile(args[0]);
	} catch (IOException ioe) {
	    System.err.println("unable to open jar file: "+args[0]);
	}

	// create ZipOutputStream
	FileOutputStream fos = null;
	try {
	    fos = new FileOutputStream(args[1]);
	} catch (IOException ioe) {
	    System.out.println("unable to create: "+args[1]);
	}
	PrintStream ps = new PrintStream(fos);
	ZipOutputStream zos = new ZipOutputStream(ps);

	// read each entry, and write it out
	for (Enumeration enum=zipFile.entries(); enum.hasMoreElements();) {
	    ZipEntry ze = (ZipEntry)enum.nextElement();
	    System.out.println(ze.getName()+": method= "+ze.getMethod()
			       +", CRC="+ze.getCrc());
	    // write entries out
	    writeEntry(zipFile, zos, ze);
	}
    }

    /*
     * Reads all the bytes for a given zip entry.
     */
    synchronized byte[] getBytes(ZipFile zf,
				 ZipEntry ze) throws IOException {
	int n;
	InputStream is = zf.getInputStream(ze);
	baos.reset();
	long left = ze.getSize();
	while((left > 0) && (n = is.read(buffer, 0, buffer.length)) != -1) {
	    baos.write(buffer, 0, n);
	    left -= n;
	}
	is.close();
	return baos.toByteArray();
    }

    void writeEntry(ZipFile zf, ZipOutputStream os, ZipEntry ze) 
    throws IOException {
	byte[] data = getBytes(zf, ze);
	os.putNextEntry(ze);
	if (data.length > 0) {
	    os.write(data);
	}
    }
}


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

WORK AROUND recalculate the crc (this is slow)
11-06-2004

EVALUATION Found bug in ZipFile.get32() method. Discussed fix with dac@eng. Will modify get32 to mask out the high-order 32 bits: * The bytes are assumed to be in Intel (little-endian) byte order. */ static final long get32(byte b[], int off) { ! return ((b[off+0] & 0xff) << 0) | ((b[off+1] & 0xff) << 8) | ! ((b[off+2] & 0xff) << 16) | ((b[off+3] & 0xff) << 24); } /* --- 385,393 ---- * The bytes are assumed to be in Intel (little-endian) byte order. */ static final long get32(byte b[], int off) { ! return (((b[off+0] & 0xff) << 0) | ((b[off+1] & 0xff) << 8) | ! ((b[off+2] & 0xff) << 16) | ((b[off+3] & 0xff) << 24)) & ! 0xffffffffL; } /* roland.schemers@Eng 1998-03-09 Verified in JDK-1.2-V build. No exceptions printed while running the given file. ###@###.### 1998-12-10
09-03-1998