1 /* 2 * Copyright (c) 2009, 2011, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import java.io.*; 27 import java.nio.*; 28 import java.util.*; 29 import java.util.zip.*; 30 31 public class LargeZip { 32 // If true, don't delete large ZIP file created for test. 33 static final boolean debug = System.getProperty("debug") != null; 34 35 //static final int DATA_LEN = 1024 * 1024; 36 static final int DATA_LEN = 80 * 1024; 37 static final int DATA_SIZE = 8; 38 39 static long fileSize = 6L * 1024L * 1024L * 1024L; // 6GB 40 41 static boolean userFile = false; 42 43 static byte[] data; 44 static File largeFile; 45 static String lastEntryName; 46 47 /* args can be empty, in which case check a 3 GB file which is created for 48 * this test (and then deleted). Or it can be a number, in which case 49 * that designates the size of the file that's created for this test (and 50 * then deleted). Or it can be the name of a file to use for the test, in 51 * which case it is *not* deleted. Note that in this last case, the data 52 * comparison might fail. 53 */ 54 static void realMain (String[] args) throws Throwable { 55 if (args.length > 0) { 56 try { 57 fileSize = Long.parseLong(args[0]); 58 System.out.println("Testing with file of size " + fileSize); 59 } catch (NumberFormatException ex) { 60 largeFile = new File(args[0]); 61 if (!largeFile.exists()) { 62 throw new Exception("Specified file " + args[0] + " does not exist"); 63 } 64 userFile = true; 65 System.out.println("Testing with user-provided file " + largeFile); 66 } 67 } 68 File testDir = null; 69 if (largeFile == null) { 70 testDir = new File(System.getProperty("test.scratch", "."), 71 "LargeZip"); 72 if (testDir.exists()) { 73 if (!testDir.delete()) { 74 throw new Exception("Cannot delete already-existing test directory"); 75 } 76 } 77 check(!testDir.exists() && testDir.mkdirs()); 78 largeFile = new File(testDir, "largezip.zip"); 79 createLargeZip(); 80 } 81 82 readLargeZip1(); 83 readLargeZip2(); 84 85 if (!userFile && !debug) { 86 check(largeFile.delete()); 87 check(testDir.delete()); 88 } 89 } 90 91 static void createLargeZip() throws Throwable { 92 int iterations = DATA_LEN / DATA_SIZE; 93 ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE); 94 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 95 for (int i = 0; i < iterations; i++) { 96 bb.putDouble(0, Math.random()); 97 baos.write(bb.array(), 0, DATA_SIZE); 98 } 99 data = baos.toByteArray(); 100 101 try (FileOutputStream fos = new FileOutputStream(largeFile); 102 BufferedOutputStream bos = new BufferedOutputStream(fos); 103 ZipOutputStream zos = new ZipOutputStream(bos)) 104 { 105 long length = 0; 106 while (length < fileSize) { 107 ZipEntry ze = new ZipEntry("entry-" + length); 108 lastEntryName = ze.getName(); 109 zos.putNextEntry(ze); 110 zos.write(data, 0, data.length); 111 zos.closeEntry(); 112 length = largeFile.length(); 113 } 114 System.out.println("Last entry written is " + lastEntryName); 115 } 116 } 117 118 static void readLargeZip1() throws Throwable { 119 ZipFile zipFile = new ZipFile(largeFile); 120 ZipEntry entry = null; 121 String entryName = null; 122 int count = 0; 123 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 124 while (entries.hasMoreElements()) { 125 entry = entries.nextElement(); 126 entryName = entry.getName(); 127 count++; 128 } 129 System.out.println("Number of entries read: " + count); 130 System.out.println("Last entry read is " + entryName); 131 check(!entry.isDirectory()); 132 if (check(entryName.equals(lastEntryName))) { 133 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 134 InputStream is = zipFile.getInputStream(entry); 135 byte buf[] = new byte[4096]; 136 int len; 137 while ((len = is.read(buf)) >= 0) { 138 baos.write(buf, 0, len); 139 } 140 baos.close(); 141 is.close(); 142 check(Arrays.equals(data, baos.toByteArray())); 143 } 144 } 145 146 147 static void readLargeZip2() throws Throwable { 148 try (FileInputStream fis = new FileInputStream(largeFile); 149 BufferedInputStream bis = new BufferedInputStream(fis); 150 ZipInputStream zis = new ZipInputStream(bis)) 151 { 152 ZipEntry entry = null; 153 String entryName = null; 154 int count = 0; 155 while ((entry = zis.getNextEntry()) != null) { 156 entryName = entry.getName(); 157 if (entryName.equals(lastEntryName)) { 158 break; 159 } 160 count++; 161 } 162 System.out.println("Number of entries read: " + count); 163 System.out.println("Last entry read is " + entryName); 164 check(!entry.isDirectory()); 165 166 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 167 168 byte buf[] = new byte[4096]; 169 int len; 170 while ((len = zis.read(buf)) >= 0) { 171 baos.write(buf, 0, len); 172 } 173 baos.close(); 174 check(Arrays.equals(data, baos.toByteArray())); 175 check(zis.getNextEntry() == null); 176 } 177 } 178 179 180 //--------------------- Infrastructure --------------------------- 181 static volatile int passed = 0, failed = 0; 182 static void pass() {passed++;} 183 static void pass(String msg) {System.out.println(msg); passed++;} 184 static void fail() {failed++; Thread.dumpStack();} 185 static void fail(String msg) {System.out.println(msg); fail();} 186 static void unexpected(Throwable t) {failed++; t.printStackTrace();} 187 static void unexpected(Throwable t, String msg) { 188 System.out.println(msg); failed++; t.printStackTrace();} 189 static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;} 190 static void equal(Object x, Object y) { 191 if (x == null ? y == null : x.equals(y)) pass(); 192 else fail(x + " not equal to " + y);} 193 public static void main(String[] args) throws Throwable { 194 try {realMain(args);} catch (Throwable t) {unexpected(t);} 195 System.out.println("\nPassed = " + passed + " failed = " + failed); 196 if (failed > 0) throw new AssertionError("Some tests failed");} 197 }