1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. 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 import java.io.*; 24 import java.util.ArrayList; 25 import java.util.Collections; 26 import java.util.Enumeration; 27 import java.util.List; 28 import java.util.jar.JarEntry; 29 import java.util.jar.JarFile; 30 import java.util.jar.JarInputStream; 31 import java.util.jar.JarOutputStream; 32 import java.util.zip.ZipEntry; 33 /* 34 * @test 35 * @bug 8029646 36 * @summary tests that native unpacker produces the same result as Java one 37 * @compile -XDignore.symbol.file Utils.java PackTestZip64.java 38 * @run main PackTestZip64 39 * @author kizune 40 */ 41 42 public class PackTestZip64 { 43 public static void main(String... args) throws Exception { 44 testPacking(); 45 Utils.cleanup(); 46 } 47 48 // 1KB buffer is enough to copy jar content 49 private static final byte[] BUFFER = new byte[1024]; 50 51 static void testPacking() throws IOException { 52 // make a copy of the test specimen to local directory 53 File testFile = new File("tools_java.jar"); 54 // Add a large number of small files to the golden jar 55 generateLargeJar(testFile, Utils.locateJar("golden.jar")); 56 57 List<String> cmdsList = new ArrayList<>(); 58 59 // Repack file to get the Java-based result 60 cmdsList.add(Utils.getPack200Cmd()); 61 cmdsList.add("--repack"); 62 cmdsList.add(testFile.getName()); 63 Utils.runExec(cmdsList); 64 cmdsList.clear(); 65 66 // Pack file with pack200 and unpack in with unpack200 67 File packedFile = new File("tools.pack.gz"); 68 cmdsList.add(Utils.getPack200Cmd()); 69 cmdsList.add(packedFile.getName()); 70 cmdsList.add(testFile.getName()); 71 Utils.runExec(cmdsList); 72 cmdsList.clear(); 73 74 File unpackedFile = new File("tools_native.jar"); 75 cmdsList.add(Utils.getUnpack200Cmd()); 76 cmdsList.add(packedFile.getName()); 77 cmdsList.add(unpackedFile.getName()); 78 Utils.runExec(cmdsList); 79 80 // Compare files binary 81 compareTwoFiles(testFile, unpackedFile); 82 83 // Cleaning up generated files 84 testFile.delete(); 85 packedFile.delete(); 86 unpackedFile.delete(); 87 } 88 89 static void compareTwoFiles(File src, File dst) throws IOException { 90 if (!src.exists()) { 91 throw new IOException("File " + src.getName() + " does not exist!"); 92 } 93 94 if(!dst.exists()) { 95 throw new IOException("File " + dst.getName() + " does not exist!"); 96 } 97 98 BufferedInputStream srcis, dstis; 99 srcis = new BufferedInputStream(new FileInputStream(src)); 100 dstis = new BufferedInputStream(new FileInputStream(dst)); 101 102 int s = 0, d, pos = 0; 103 while (s != -1) { // Checking of just one result for EOF is enough 104 s = srcis.read(); 105 d = dstis.read(); 106 107 if (s != d) { 108 throw new IOException("Files are differ starting at position: " 109 + Integer.toHexString(pos)); 110 } 111 112 pos++; 113 } 114 115 srcis.close(); 116 dstis.close(); 117 } 118 119 static void generateLargeJar(File result, File source) throws IOException { 120 if (result.exists()) { 121 result.delete(); 122 } 123 124 try (JarOutputStream copyTo = new JarOutputStream(new FileOutputStream(result)); 125 JarFile srcJar = new JarFile(source)) { 126 127 for (JarEntry je : Collections.list(srcJar.entries())) { 128 copyTo.putNextEntry(je); 129 if (!je.isDirectory()) { 130 copyStream(srcJar.getInputStream(je), copyTo); 131 } 132 copyTo.closeEntry(); 133 } 134 135 int many = Short.MAX_VALUE * 2 + 2; 136 137 for (int i = 0 ; i < many ; i++) { 138 JarEntry e = new JarEntry("F-" + i + ".txt"); 139 copyTo.putNextEntry(e); 140 } 141 copyTo.flush(); 142 copyTo.close(); 143 } 144 } 145 146 static void copyStream(InputStream in, OutputStream out) throws IOException { 147 int bytesRead; 148 while ((bytesRead = in.read(BUFFER))!= -1) { 149 out.write(BUFFER, 0, bytesRead); 150 } 151 } 152 }