# HG changeset patch # User martin # Date 1364330094 25200 # Node ID 7824dded98216e737baad7d2cce5be60309b2208 # Parent 9238cf6209b36d3d77651f2db180e3e7a29b5bff 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k Summary: Provide a system property to inhibit ZIP64 mode for >64k entries Reviewed-by: alanb, sherman diff -r 9238cf6209b3 -r 7824dded9821 jdk/src/share/classes/java/util/zip/ZipOutputStream.java --- a/jdk/src/share/classes/java/util/zip/ZipOutputStream.java Mon Mar 25 18:14:52 2013 -0700 +++ b/jdk/src/share/classes/java/util/zip/ZipOutputStream.java Tue Mar 26 13:34:54 2013 -0700 @@ -43,6 +43,20 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstants { + /** + * Whether to use ZIP64 for zip files with more than 64k entries. + * Until ZIP64 support in zip implementations is ubiquitous, this + * system property allows the creation of zip files which can be + * read by legacy zip implementations which tolerate "incorrect" + * total entry count fields, such as the ones in jdk6, and even + * some in jdk7. + */ + private static final boolean inhibitZip64 = + Boolean.parseBoolean( + java.security.AccessController.doPrivileged( + new sun.security.action.GetPropertyAction( + "jdk.util.zip.inhibitZip64", "false"))); + private static class XEntry { public final ZipEntry entry; public final long offset; @@ -534,8 +548,10 @@ } int count = xentries.size(); if (count >= ZIP64_MAGICCOUNT) { - count = ZIP64_MAGICCOUNT; - hasZip64 = true; + hasZip64 |= !inhibitZip64; + if (hasZip64) { + count = ZIP64_MAGICCOUNT; + } } if (hasZip64) { long off64 = written; diff -r 9238cf6209b3 -r 7824dded9821 jdk/test/java/util/zip/EntryCount64k.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/util/zip/EntryCount64k.java Tue Mar 26 13:34:54 2013 -0700 @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2013 Google Inc. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @summary Test java.util.zip behavior with ~64k entries + * @run main/othervm EntryCount64k + * @run main/othervm -Djdk.util.zip.inhibitZip64=true EntryCount64k + * @run main/othervm -Djdk.util.zip.inhibitZip64=false EntryCount64k + */ + +import java.io.*; +import java.util.*; +import java.util.zip.*; + +public class EntryCount64k { + + public static void main(String[] args) throws Exception { + for (int i = (1 << 16) - 2; i < (1 << 16) + 2; i++) + test(i); + } + + static void test(int entryCount) throws Exception { + File zipFile = new File("EntryCount64k-tmp.zip"); + zipFile.delete(); + + try (ZipOutputStream zos = + new ZipOutputStream( + new BufferedOutputStream( + new FileOutputStream(zipFile)))) { + for (int i = 0; i < entryCount; i++) { + ZipEntry e = new ZipEntry(Integer.toString(i)); + zos.putNextEntry(e); + zos.closeEntry(); + } + } + + String p = System.getProperty("jdk.util.zip.inhibitZip64"); + boolean tooManyEntries = entryCount >= (1 << 16) - 1; + boolean shouldUseZip64 = tooManyEntries & !("true".equals(p)); + boolean usesZip64 = usesZip64(zipFile); + String details = String.format + ("entryCount=%d shouldUseZip64=%s usesZip64=%s zipSize=%d%n", + entryCount, shouldUseZip64, usesZip64, zipFile.length()); + System.err.println(details); + checkCanRead(zipFile, entryCount); + if (shouldUseZip64 != usesZip64) + throw new Error(details); + zipFile.delete(); + } + + static boolean usesZip64(File zipFile) throws Exception { + RandomAccessFile raf = new RandomAccessFile(zipFile, "r"); + byte[] buf = new byte[4096]; + raf.seek(raf.length() - buf.length); + raf.read(buf); + for (int i = 0; i < buf.length - 4; i++) { + // Look for ZIP64 End Header Signature + // Phil Katz: yes, we will always remember you + if (buf[i+0] == 'P' && + buf[i+1] == 'K' && + buf[i+2] == 6 && + buf[i+3] == 6) + return true; + } + return false; + } + + static void checkCanRead(File zipFile, int entryCount) throws Exception { + // Check ZipInputStream API + try (ZipInputStream zis = + new ZipInputStream( + new BufferedInputStream( + new FileInputStream(zipFile)))) { + for (int i = 0; i < entryCount; i++) { + ZipEntry e = zis.getNextEntry(); + if (Integer.parseInt(e.getName()) != i) + throw new AssertionError(); + } + if (zis.getNextEntry() != null) + throw new AssertionError(); + } + + // Check ZipFile API + try (ZipFile zf = new ZipFile(zipFile)) { + Enumeration en = zf.entries(); + for (int i = 0; i < entryCount; i++) { + ZipEntry e = en.nextElement(); + if (Integer.parseInt(e.getName()) != i) + throw new AssertionError(); + } + if (en.hasMoreElements() + || (zf.size() != entryCount) + || (zf.getEntry(Integer.toString(entryCount - 1)) == null) + || (zf.getEntry(Integer.toString(entryCount)) != null)) + throw new AssertionError(); + } + } +}