test/java/util/zip/ZipFile/ManyEntries.java

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

@@ -53,14 +53,14 @@
         long time = System.currentTimeMillis() / 2000 * 2000;
         CRC32 crc32 = new CRC32();
         File zipFile = new File(++uniquifier + ".zip");
         try {
             zipFile.delete();
-            ZipOutputStream zos = new ZipOutputStream(
-                new BufferedOutputStream(
-                    new FileOutputStream(zipFile)));
-            try {
+            try (FileOutputStream fos = new FileOutputStream(zipFile);
+                 BufferedOutputStream bos = new BufferedOutputStream(fos);
+                 ZipOutputStream zos = new ZipOutputStream(bos))
+            {
                 for (int i = 0; i < N; i++) {
                     ZipEntry e = new ZipEntry("DIR/"+i);
                     e.setMethod(method);
                     e.setTime(time);
                     if (method == ZipEntry.STORED) {

@@ -73,17 +73,13 @@
                         e.setCrc(0);
                     }
                     zos.putNextEntry(e);
                     zos.write(i);
                 }
-            } finally {
-                zos.close();
-                zos = null;
             }
 
-            ZipFile zip = zip = new ZipFile(zipFile);
-            try {
+            try (ZipFile zip = new ZipFile(zipFile)) {
                 if (! (zip.size() == N))
                     throw new Exception("Bad ZipFile size: " + zip.size());
                 Enumeration entries = zip.entries();
 
                 for (int i = 0; i < N; i++) {

@@ -102,14 +98,11 @@
                     if (! (zip.getInputStream(e).read() == (i & 0xff)))
                         throw new Exception("Bad file contents: " + i);
                 }
                 if (entries.hasMoreElements())
                     throw new Exception("too many elements");
-            } finally {
-                zip.close();
-            }
         }
-        finally {
+        } finally {
             zipFile.delete();
         }
     }
 }