jdk/test/java/util/zip/ZipFile/ManyEntries.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4770745 4828461
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary open zip files with more than 64k entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run main/timeout=600 ManyEntries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class ManyEntries {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        boolean runMoreTests = args.length > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        int[] sizes = (runMoreTests ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                       new int[]{1, 2, 42, 65534, 65535, 65536, 65537, 68000} :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                       new int[]{1, 2, 42, 68000});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        int[] methods = {ZipEntry.STORED, ZipEntry.DEFLATED};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        for (int size : sizes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            for (int method : methods)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                test(size, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // JDK doesn't like having zip file contents changed at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static int uniquifier = 42;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static void test(int N, int method) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        System.out.println("N="+N+", method="+method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        long time = System.currentTimeMillis() / 2000 * 2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        CRC32 crc32 = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        File zipFile = new File(++uniquifier + ".zip");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            zipFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            ZipOutputStream zos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                new ZipOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                        new FileOutputStream(zipFile)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            for (int i = 0; i < N; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                ZipEntry e = new ZipEntry("DIR/"+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                e.setMethod(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                e.setTime(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                if (method == ZipEntry.STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    e.setSize(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    crc32.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    crc32.update((byte)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    e.setCrc(crc32.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    e.setSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    e.setCrc(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                zos.putNextEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                zos.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            zos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            ZipFile zip = new ZipFile(zipFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (! (zip.size() == N))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                throw new Exception("Bad ZipFile size: " + zip.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            Enumeration entries = zip.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            for (int i = 0; i < N; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                if (i % 1000 == 0) {System.gc(); System.runFinalization();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (! (entries.hasMoreElements()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    throw new Exception("only " + i + " elements");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                ZipEntry e = (ZipEntry)entries.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (! (e.getSize() == 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    throw new Exception("bad size: " + e.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (! (e.getName().equals("DIR/" + i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    throw new Exception("bad name: " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (! (e.getMethod() == method))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    throw new Exception("getMethod="+e.getMethod()+", method=" + method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (! (e.getTime() == time))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    throw new Exception("getTime="+e.getTime()+", time=" + time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (! (zip.getInputStream(e).read() == (i & 0xff)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    throw new Exception("Bad file contents: " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (entries.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                throw new Exception("too many elements");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            zipFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
}