jdk/test/java/util/zip/EntryCount64k.java
author martin
Tue, 26 Mar 2013 13:34:54 -0700
changeset 16713 7824dded9821
child 29713 eeabfe673c97
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16713
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     1
/*
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     2
 * Copyright (c) 2013 Google Inc. All rights reserved.
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     4
 *
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     8
 *
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    13
 * accompanied this code).
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    14
 *
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    18
 *
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    21
 * questions.
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    22
 */
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    23
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    24
/**
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    25
 * @test
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    26
 * @summary Test java.util.zip behavior with ~64k entries
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    27
 * @run main/othervm EntryCount64k
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    28
 * @run main/othervm -Djdk.util.zip.inhibitZip64=true EntryCount64k
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    29
 * @run main/othervm -Djdk.util.zip.inhibitZip64=false EntryCount64k
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    30
 */
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    31
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    32
import java.io.*;
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    33
import java.util.*;
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    34
import java.util.zip.*;
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    35
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    36
public class EntryCount64k {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    37
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    39
        for (int i = (1 << 16) - 2; i < (1 << 16) + 2; i++)
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    40
            test(i);
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    41
    }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    42
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    43
    static void test(int entryCount) throws Exception {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    44
        File zipFile = new File("EntryCount64k-tmp.zip");
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    45
        zipFile.delete();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    46
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    47
        try (ZipOutputStream zos =
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    48
             new ZipOutputStream(
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    49
                new BufferedOutputStream(
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    50
                    new FileOutputStream(zipFile)))) {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    51
            for (int i = 0; i < entryCount; i++) {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    52
                ZipEntry e = new ZipEntry(Integer.toString(i));
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    53
                zos.putNextEntry(e);
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    54
                zos.closeEntry();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    55
            }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    56
        }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    57
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    58
        String p = System.getProperty("jdk.util.zip.inhibitZip64");
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    59
        boolean tooManyEntries = entryCount >= (1 << 16) - 1;
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    60
        boolean shouldUseZip64 = tooManyEntries & !("true".equals(p));
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    61
        boolean usesZip64 = usesZip64(zipFile);
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    62
        String details = String.format
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    63
            ("entryCount=%d shouldUseZip64=%s usesZip64=%s zipSize=%d%n",
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    64
             entryCount, shouldUseZip64, usesZip64, zipFile.length());
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    65
        System.err.println(details);
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    66
        checkCanRead(zipFile, entryCount);
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    67
        if (shouldUseZip64 != usesZip64)
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    68
            throw new Error(details);
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    69
        zipFile.delete();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    70
    }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    71
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    72
    static boolean usesZip64(File zipFile) throws Exception {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    73
        RandomAccessFile raf = new RandomAccessFile(zipFile, "r");
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    74
        byte[] buf = new byte[4096];
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    75
        raf.seek(raf.length() - buf.length);
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    76
        raf.read(buf);
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    77
        for (int i = 0; i < buf.length - 4; i++) {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    78
            // Look for ZIP64 End Header Signature
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    79
            // Phil Katz: yes, we will always remember you
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    80
            if (buf[i+0] == 'P' &&
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    81
                buf[i+1] == 'K' &&
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    82
                buf[i+2] == 6   &&
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    83
                buf[i+3] == 6)
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    84
                return true;
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    85
        }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    86
        return false;
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    87
    }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    88
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    89
    static void checkCanRead(File zipFile, int entryCount) throws Exception {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    90
        // Check ZipInputStream API
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    91
        try (ZipInputStream zis =
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    92
             new ZipInputStream(
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    93
                 new BufferedInputStream(
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    94
                     new FileInputStream(zipFile)))) {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    95
            for (int i = 0; i < entryCount; i++) {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    96
                ZipEntry e = zis.getNextEntry();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    97
                if (Integer.parseInt(e.getName()) != i)
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    98
                    throw new AssertionError();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
    99
            }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   100
            if (zis.getNextEntry() != null)
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   101
                throw new AssertionError();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   102
        }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   103
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   104
        // Check ZipFile API
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   105
        try (ZipFile zf = new ZipFile(zipFile)) {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   106
            Enumeration<? extends ZipEntry> en = zf.entries();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   107
            for (int i = 0; i < entryCount; i++) {
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   108
                ZipEntry e = en.nextElement();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   109
                if (Integer.parseInt(e.getName()) != i)
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   110
                    throw new AssertionError();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   111
            }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   112
            if (en.hasMoreElements()
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   113
                || (zf.size() != entryCount)
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   114
                || (zf.getEntry(Integer.toString(entryCount - 1)) == null)
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   115
                || (zf.getEntry(Integer.toString(entryCount)) != null))
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   116
                throw new AssertionError();
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   117
        }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   118
    }
7824dded9821 8007905: To add a system property to create zip file without using ZIP64 end table when entry count > 64k
martin
parents:
diff changeset
   119
}