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