test/java/util/zip/ZipFile/LargeZipFile.java

Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: XXX


  76             createLargeZip();
  77         }
  78 
  79         readLargeZip();
  80 
  81         if (!userFile && !debug) {
  82             check(largeFile.delete());
  83             check(testDir.delete());
  84         }
  85     }
  86 
  87     static void createLargeZip() throws Throwable {
  88         int iterations = DATA_LEN / DATA_SIZE;
  89         ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE);
  90         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  91         for (int i = 0; i < iterations; i++) {
  92             bb.putDouble(0, Math.random());
  93             baos.write(bb.array(), 0, DATA_SIZE);
  94         }
  95         data = baos.toByteArray();
  96 
  97         ZipOutputStream zos = new ZipOutputStream(
  98             new BufferedOutputStream(new FileOutputStream(largeFile)));

  99         long length = 0;
 100         while (length < fileSize) {
 101             ZipEntry ze = new ZipEntry("entry-" + length);
 102             lastEntryName = ze.getName();
 103             zos.putNextEntry(ze);
 104             zos.write(data, 0, data.length);
 105             zos.closeEntry();
 106             length = largeFile.length();
 107         }
 108         System.out.println("Last entry written is " + lastEntryName);
 109         zos.close();
 110     }
 111 
 112     static void readLargeZip() throws Throwable {
 113         ZipFile zipFile = new ZipFile(largeFile);
 114         ZipEntry entry = null;
 115         String entryName = null;
 116         int count = 0;
 117         Enumeration<? extends ZipEntry> entries = zipFile.entries();
 118         while (entries.hasMoreElements()) {
 119             entry = entries.nextElement();
 120             entryName = entry.getName();
 121             count++;
 122         }
 123         System.out.println("Number of entries read: " + count);
 124         System.out.println("Last entry read is " + entryName);
 125         check(!entry.isDirectory());
 126         if (check(entryName.equals(lastEntryName))) {
 127             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 128             InputStream is = zipFile.getInputStream(entry);
 129             byte buf[] = new byte[4096];
 130             int len;
 131             while ((len = is.read(buf)) >= 0) {
 132                 baos.write(buf, 0, len);
 133             }
 134             baos.close();
 135             is.close();
 136             check(Arrays.equals(data, baos.toByteArray()));
 137         }
 138         try {
 139           zipFile.close();
 140         } catch (IOException ioe) {/* what can you do */ }
 141     }
 142 
 143     //--------------------- Infrastructure ---------------------------
 144     static volatile int passed = 0, failed = 0;
 145     static void pass() {passed++;}
 146     static void pass(String msg) {System.out.println(msg); passed++;}
 147     static void fail() {failed++; Thread.dumpStack();}
 148     static void fail(String msg) {System.out.println(msg); fail();}
 149     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 150     static void unexpected(Throwable t, String msg) {
 151         System.out.println(msg); failed++; t.printStackTrace();}
 152     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
 153     static void equal(Object x, Object y) {
 154         if (x == null ? y == null : x.equals(y)) pass();
 155         else fail(x + " not equal to " + y);}
 156     public static void main(String[] args) throws Throwable {
 157         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 158         System.out.println("\nPassed = " + passed + " failed = " + failed);
 159         if (failed > 0) throw new AssertionError("Some tests failed");}
 160 }


  76             createLargeZip();
  77         }
  78 
  79         readLargeZip();
  80 
  81         if (!userFile && !debug) {
  82             check(largeFile.delete());
  83             check(testDir.delete());
  84         }
  85     }
  86 
  87     static void createLargeZip() throws Throwable {
  88         int iterations = DATA_LEN / DATA_SIZE;
  89         ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE);
  90         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  91         for (int i = 0; i < iterations; i++) {
  92             bb.putDouble(0, Math.random());
  93             baos.write(bb.array(), 0, DATA_SIZE);
  94         }
  95         data = baos.toByteArray();
  96         try (FileOutputStream fos = new FileOutputStream(largeFile);
  97              BufferedOutputStream bos = new BufferedOutputStream(fos);
  98              ZipOutputStream zos = new ZipOutputStream(bos))
  99         {
 100             long length = 0;
 101             while (length < fileSize) {
 102                 ZipEntry ze = new ZipEntry("entry-" + length);
 103                 lastEntryName = ze.getName();
 104                 zos.putNextEntry(ze);
 105                 zos.write(data, 0, data.length);
 106                 zos.closeEntry();
 107                 length = largeFile.length();
 108             }
 109             System.out.println("Last entry written is " + lastEntryName);
 110         }
 111     }
 112 
 113     static void readLargeZip() throws Throwable {
 114         try (ZipFile zipFile = new ZipFile(largeFile)) {
 115             ZipEntry entry = null;
 116             String entryName = null;
 117             int count = 0;
 118             Enumeration<? extends ZipEntry> entries = zipFile.entries();
 119             while (entries.hasMoreElements()) {
 120                 entry = entries.nextElement();
 121                 entryName = entry.getName();
 122                 count++;
 123             }
 124             System.out.println("Number of entries read: " + count);
 125             System.out.println("Last entry read is " + entryName);
 126             check(!entry.isDirectory());
 127             if (check(entryName.equals(lastEntryName))) {
 128                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 129                 InputStream is = zipFile.getInputStream(entry);
 130                 byte buf[] = new byte[4096];
 131                 int len;
 132                 while ((len = is.read(buf)) >= 0) {
 133                     baos.write(buf, 0, len);
 134                 }
 135                 baos.close();
 136                 is.close();
 137                 check(Arrays.equals(data, baos.toByteArray()));
 138             }
 139         }


 140     }
 141 
 142     //--------------------- Infrastructure ---------------------------
 143     static volatile int passed = 0, failed = 0;
 144     static void pass() {passed++;}
 145     static void pass(String msg) {System.out.println(msg); passed++;}
 146     static void fail() {failed++; Thread.dumpStack();}
 147     static void fail(String msg) {System.out.println(msg); fail();}
 148     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 149     static void unexpected(Throwable t, String msg) {
 150         System.out.println(msg); failed++; t.printStackTrace();}
 151     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
 152     static void equal(Object x, Object y) {
 153         if (x == null ? y == null : x.equals(y)) pass();
 154         else fail(x + " not equal to " + y);}
 155     public static void main(String[] args) throws Throwable {
 156         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 157         System.out.println("\nPassed = " + passed + " failed = " + failed);
 158         if (failed > 0) throw new AssertionError("Some tests failed");}
 159 }