jdk/test/java/util/zip/LargeZip.java
changeset 2438 21c111b51aa8
child 5506 202f599c92aa
equal deleted inserted replaced
2436:ae4667371301 2438:21c111b51aa8
       
     1 /*
       
     2  * Copyright 2009 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any 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         ZipOutputStream zos = new ZipOutputStream(
       
   102             new BufferedOutputStream(new FileOutputStream(largeFile)));
       
   103         long length = 0;
       
   104         while (length < fileSize) {
       
   105             ZipEntry ze = new ZipEntry("entry-" + length);
       
   106             lastEntryName = ze.getName();
       
   107             zos.putNextEntry(ze);
       
   108             zos.write(data, 0, data.length);
       
   109             zos.closeEntry();
       
   110             length = largeFile.length();
       
   111         }
       
   112         System.out.println("Last entry written is " + lastEntryName);
       
   113         zos.close();
       
   114     }
       
   115 
       
   116     static void readLargeZip1() throws Throwable {
       
   117         ZipFile zipFile = new ZipFile(largeFile);
       
   118         ZipEntry entry = null;
       
   119         String entryName = null;
       
   120         int count = 0;
       
   121         Enumeration<? extends ZipEntry> entries = zipFile.entries();
       
   122         while (entries.hasMoreElements()) {
       
   123             entry = entries.nextElement();
       
   124             entryName = entry.getName();
       
   125             count++;
       
   126         }
       
   127         System.out.println("Number of entries read: " + count);
       
   128         System.out.println("Last entry read is " + entryName);
       
   129         check(!entry.isDirectory());
       
   130         if (check(entryName.equals(lastEntryName))) {
       
   131             ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
   132             InputStream is = zipFile.getInputStream(entry);
       
   133             byte buf[] = new byte[4096];
       
   134             int len;
       
   135             while ((len = is.read(buf)) >= 0) {
       
   136                 baos.write(buf, 0, len);
       
   137             }
       
   138             baos.close();
       
   139             is.close();
       
   140             check(Arrays.equals(data, baos.toByteArray()));
       
   141         }
       
   142     }
       
   143 
       
   144 
       
   145     static void readLargeZip2() throws Throwable {
       
   146         ZipInputStream zis = new ZipInputStream(
       
   147             new BufferedInputStream(new FileInputStream(largeFile)));
       
   148         ZipEntry entry = null;
       
   149         String entryName = null;
       
   150         int count = 0;
       
   151         while ((entry = zis.getNextEntry()) != null) {
       
   152             entryName = entry.getName();
       
   153             if (entryName.equals(lastEntryName)) {
       
   154                 break;
       
   155             }
       
   156             count++;
       
   157         }
       
   158         System.out.println("Number of entries read: " + count);
       
   159         System.out.println("Last entry read is " + entryName);
       
   160         check(!entry.isDirectory());
       
   161 
       
   162         ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
   163 
       
   164         byte buf[] = new byte[4096];
       
   165         int len;
       
   166         while ((len = zis.read(buf)) >= 0) {
       
   167             baos.write(buf, 0, len);
       
   168         }
       
   169         baos.close();
       
   170         check(Arrays.equals(data, baos.toByteArray()));
       
   171         check(zis.getNextEntry() == null);
       
   172         zis.close();
       
   173     }
       
   174 
       
   175 
       
   176     //--------------------- Infrastructure ---------------------------
       
   177     static volatile int passed = 0, failed = 0;
       
   178     static void pass() {passed++;}
       
   179     static void pass(String msg) {System.out.println(msg); passed++;}
       
   180     static void fail() {failed++; Thread.dumpStack();}
       
   181     static void fail(String msg) {System.out.println(msg); fail();}
       
   182     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
       
   183     static void unexpected(Throwable t, String msg) {
       
   184         System.out.println(msg); failed++; t.printStackTrace();}
       
   185     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
       
   186     static void equal(Object x, Object y) {
       
   187         if (x == null ? y == null : x.equals(y)) pass();
       
   188         else fail(x + " not equal to " + y);}
       
   189     public static void main(String[] args) throws Throwable {
       
   190         try {realMain(args);} catch (Throwable t) {unexpected(t);}
       
   191         System.out.println("\nPassed = " + passed + " failed = " + failed);
       
   192         if (failed > 0) throw new AssertionError("Some tests failed");}
       
   193 }