test/java/util/zip/ZipFile/ShortRead.java

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

@@ -36,18 +36,19 @@
     public static void main(String[] args) throws Exception {
         final File zFile = new File("abc.zip");
         try {
             final String entryName = "abc";
             final String data = "Data disponible";
-            final ZipOutputStream zos =
-                new ZipOutputStream(new FileOutputStream(zFile));
+            try (FileOutputStream fos = new FileOutputStream(zFile);
+                 ZipOutputStream zos = new ZipOutputStream(fos))
+            {
             zos.putNextEntry(new ZipEntry(entryName));
             zos.write(data.getBytes("ASCII"));
             zos.closeEntry();
-            zos.close();
+            }
 
-            final ZipFile zipFile = new ZipFile(zFile);
+            try (ZipFile zipFile = new ZipFile(zFile)) {
             final ZipEntry zentry = zipFile.getEntry(entryName);
             final InputStream inputStream = zipFile.getInputStream(zentry);
             System.out.printf("size=%d csize=%d available=%d%n",
                               zentry.getSize(),
                               zentry.getCompressedSize(),

@@ -55,10 +56,11 @@
             byte[] buf = new byte[data.length()];
             final int count = inputStream.read(buf);
             if (! new String(buf, "ASCII").equals(data) ||
                 count != data.length())
                 throw new Exception("short read?");
-            zipFile.close();
         }
-        finally { zFile.delete(); }
+        } finally {
+            zFile.delete();
+        }
     }
 }