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