jdk/test/tools/pack200/PackTestZip64.java
author kizune
Fri, 24 Jan 2014 21:29:25 +0400
changeset 22575 0c2768190cf4
child 28255 ddb0157601ed
permissions -rw-r--r--
8029646: [pack200] should support the new zip64 format. Reviewed-by: ksrini, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22575
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     1
/*
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     4
 *
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     8
 *
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    13
 * accompanied this code).
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    14
 *
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    18
 *
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    21
 * questions.
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    22
 */
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    23
import java.io.*;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    24
import java.util.ArrayList;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    25
import java.util.Collections;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    26
import java.util.Enumeration;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    27
import java.util.List;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    28
import java.util.jar.JarEntry;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    29
import java.util.jar.JarFile;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    30
import java.util.jar.JarInputStream;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    31
import java.util.jar.JarOutputStream;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    32
import java.util.zip.ZipEntry;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    33
/*
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    34
 * @test
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    35
 * @bug 8029646
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    36
 * @summary tests that native unpacker produces the same result as Java one
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    37
 * @compile -XDignore.symbol.file Utils.java PackTestZip64.java
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    38
 * @run main PackTestZip64
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    39
 * @author kizune
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    40
 */
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    41
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    42
public class PackTestZip64 {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    43
    public static void main(String... args) throws Exception {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    44
        testPacking();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    45
        Utils.cleanup();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    46
    }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    47
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    48
    // 1KB buffer is enough to copy jar content
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    49
    private static final byte[] BUFFER = new byte[1024];
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    50
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    51
    static void testPacking() throws IOException {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    52
        // make a copy of the test specimen to local directory
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    53
        File testFile = new File("tools_java.jar");
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    54
        // Add a large number of small files to the golden jar
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    55
        generateLargeJar(testFile, Utils.locateJar("golden.jar"));
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    56
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    57
        List<String> cmdsList = new ArrayList<>();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    58
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    59
        // Repack file to get the Java-based result
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    60
        cmdsList.add(Utils.getPack200Cmd());
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    61
        cmdsList.add("--repack");
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    62
        cmdsList.add(testFile.getName());
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    63
        Utils.runExec(cmdsList);
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    64
        cmdsList.clear();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    65
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    66
        // Pack file with pack200 and unpack in with unpack200
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    67
        File packedFile = new File("tools.pack.gz");
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    68
        cmdsList.add(Utils.getPack200Cmd());
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    69
        cmdsList.add(packedFile.getName());
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    70
        cmdsList.add(testFile.getName());
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    71
        Utils.runExec(cmdsList);
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    72
        cmdsList.clear();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    73
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    74
        File unpackedFile = new File("tools_native.jar");
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    75
        cmdsList.add(Utils.getUnpack200Cmd());
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    76
        cmdsList.add(packedFile.getName());
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    77
        cmdsList.add(unpackedFile.getName());
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    78
        Utils.runExec(cmdsList);
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    79
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    80
        // Compare files binary
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    81
        compareTwoFiles(testFile, unpackedFile);
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    82
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    83
        // Cleaning up generated files
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    84
        testFile.delete();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    85
        packedFile.delete();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    86
        unpackedFile.delete();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    87
    }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    88
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    89
    static void compareTwoFiles(File src, File dst) throws IOException {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    90
        if (!src.exists()) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    91
            throw new IOException("File " + src.getName() + " does not exist!");
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    92
        }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    93
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    94
        if(!dst.exists()) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    95
            throw new IOException("File " + dst.getName() + " does not exist!");
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    96
        }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    97
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    98
        BufferedInputStream srcis, dstis;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
    99
        srcis = new BufferedInputStream(new FileInputStream(src));
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   100
        dstis = new BufferedInputStream(new FileInputStream(dst));
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   101
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   102
        int s = 0, d, pos = 0;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   103
        while (s != -1) { // Checking of just one result for EOF is enough
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   104
            s = srcis.read();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   105
            d = dstis.read();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   106
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   107
            if (s != d) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   108
                throw new IOException("Files are differ starting at position: "
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   109
                + Integer.toHexString(pos));
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   110
            }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   111
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   112
            pos++;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   113
        }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   114
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   115
        srcis.close();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   116
        dstis.close();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   117
    }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   118
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   119
    static void generateLargeJar(File result, File source) throws IOException {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   120
        if (result.exists()) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   121
            result.delete();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   122
        }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   123
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   124
        try (JarOutputStream copyTo = new JarOutputStream(new FileOutputStream(result));
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   125
             JarFile srcJar = new JarFile(source)) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   126
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   127
            for (JarEntry je : Collections.list(srcJar.entries())) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   128
                copyTo.putNextEntry(je);
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   129
                if (!je.isDirectory()) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   130
                    copyStream(srcJar.getInputStream(je), copyTo);
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   131
                }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   132
                copyTo.closeEntry();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   133
            }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   134
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   135
            int many = Short.MAX_VALUE * 2 + 2;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   136
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   137
            for (int i = 0 ; i < many ; i++) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   138
                JarEntry e = new JarEntry("F-" + i + ".txt");
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   139
                copyTo.putNextEntry(e);
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   140
            }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   141
            copyTo.flush();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   142
            copyTo.close();
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   143
        }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   144
    }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   145
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   146
    static void copyStream(InputStream in, OutputStream out) throws IOException {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   147
        int bytesRead;
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   148
        while ((bytesRead = in.read(BUFFER))!= -1) {
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   149
            out.write(BUFFER, 0, bytesRead);
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   150
        }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   151
    }
0c2768190cf4 8029646: [pack200] should support the new zip64 format.
kizune
parents:
diff changeset
   152
}