test/java/util/zip/InfoZip.java

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


  68         return sb.toString();
  69     }
  70 
  71     private static void checkZipEntry(ZipEntry ze, String contents) {
  72         check(ze.getName().equals("someFile"), "filename");
  73         check(ze.getExtra() != null, "extra");
  74         check(contents.equals("Message in a Bottle\n"), "contents");
  75         check(ze.getSize() == "Message in a Bottle\n".length());
  76     }
  77 
  78 
  79     public static void main(String[] args) throws Exception {
  80         //----------------------------------------------------------------
  81         // The file InfoZip.zip was created using Unix Info-Zip's zip, thus:
  82         // echo Message in a Bottle > someFile; zip InfoZip.zip someFile
  83         // Such a file has a LOC extra different from the CEN extra,
  84         // which cannot happen using JDK APIs.
  85         //----------------------------------------------------------------
  86         File f = new File("InfoZip.zip");
  87 
  88         OutputStream os = new FileOutputStream(f);
  89         os.write(new byte[]
  90             {'P', 'K', 3, 4, 10, 0, 0, 0, 0, 0, -68, 8, 'k',
  91              '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20, 0, 0, 0,
  92              8, 0, 21, 0, 's', 'o', 'm', 'e', 'F', 'i', 'l', 'e', 'U',
  93              'T', 9, 0, 3, 't', '_', '1', 'B', 't', '_', '1', 'B', 'U',
  94              'x', 4, 0, -14, 'v', 26, 4, 'M', 'e', 's', 's', 'a', 'g',
  95              'e', ' ', 'i', 'n', ' ', 'a', ' ', 'B', 'o', 't', 't', 'l', 'e',
  96              10, 'P', 'K', 1, 2, 23, 3, 10, 0, 0, 0, 0, 0,
  97              -68, 8, 'k', '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20,
  98              0, 0, 0, 8, 0, 13, 0, 0, 0, 0, 0, 1, 0,
  99              0, 0, -92, -127, 0, 0, 0, 0, 's', 'o', 'm', 'e', 'F',
 100              'i', 'l', 'e', 'U', 'T', 5, 0, 3, 't', '_', '1', 'B', 'U',
 101              'x', 0, 0, 'P', 'K', 5, 6, 0, 0, 0, 0, 1, 0,
 102              1, 0, 'C', 0, 0, 0, 'O', 0, 0, 0, 0, 0, });
 103         os.close();
 104 
 105         ZipFile zf = new ZipFile(f);
 106         ZipEntry ze = null;
 107         try {
 108             Enumeration<? extends ZipEntry> entries = zf.entries();
 109             ze = entries.nextElement();
 110             check(! entries.hasMoreElements());
 111             checkZipEntry(ze, contents(zf, ze));
 112         } finally {
 113             zf.close();
 114         }
 115 
 116         ZipInputStream is = new ZipInputStream(new FileInputStream(f));
 117         try {

 118             ze = is.getNextEntry();
 119             checkZipEntry(ze, contents(is));
 120             check(is.getNextEntry() == null);
 121         } finally {
 122             is.close();
 123         }
 124         f.delete();
 125         System.out.printf("passed = %d, failed = %d%n", passed, failed);
 126         if (failed > 0) throw new Exception("Some tests failed");
 127     }
 128 }


  68         return sb.toString();
  69     }
  70 
  71     private static void checkZipEntry(ZipEntry ze, String contents) {
  72         check(ze.getName().equals("someFile"), "filename");
  73         check(ze.getExtra() != null, "extra");
  74         check(contents.equals("Message in a Bottle\n"), "contents");
  75         check(ze.getSize() == "Message in a Bottle\n".length());
  76     }
  77 
  78 
  79     public static void main(String[] args) throws Exception {
  80         //----------------------------------------------------------------
  81         // The file InfoZip.zip was created using Unix Info-Zip's zip, thus:
  82         // echo Message in a Bottle > someFile; zip InfoZip.zip someFile
  83         // Such a file has a LOC extra different from the CEN extra,
  84         // which cannot happen using JDK APIs.
  85         //----------------------------------------------------------------
  86         File f = new File("InfoZip.zip");
  87 
  88         try (OutputStream os = new FileOutputStream(f)) {
  89             os.write(new byte[]
  90                 {'P', 'K', 3, 4, 10, 0, 0, 0, 0, 0, -68, 8, 'k',
  91                  '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20, 0, 0, 0,
  92                  8, 0, 21, 0, 's', 'o', 'm', 'e', 'F', 'i', 'l', 'e', 'U',
  93                  'T', 9, 0, 3, 't', '_', '1', 'B', 't', '_', '1', 'B', 'U',
  94                  'x', 4, 0, -14, 'v', 26, 4, 'M', 'e', 's', 's', 'a', 'g',
  95                  'e', ' ', 'i', 'n', ' ', 'a', ' ', 'B', 'o', 't', 't', 'l', 'e',
  96                  10, 'P', 'K', 1, 2, 23, 3, 10, 0, 0, 0, 0, 0,
  97                  -68, 8, 'k', '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20,
  98                  0, 0, 0, 8, 0, 13, 0, 0, 0, 0, 0, 1, 0,
  99                  0, 0, -92, -127, 0, 0, 0, 0, 's', 'o', 'm', 'e', 'F',
 100                  'i', 'l', 'e', 'U', 'T', 5, 0, 3, 't', '_', '1', 'B', 'U',
 101                  'x', 0, 0, 'P', 'K', 5, 6, 0, 0, 0, 0, 1, 0,
 102                  1, 0, 'C', 0, 0, 0, 'O', 0, 0, 0, 0, 0, });
 103         }
 104 

 105         ZipEntry ze = null;
 106         try (ZipFile zf = new ZipFile(f)) {
 107             Enumeration<? extends ZipEntry> entries = zf.entries();
 108             ze = entries.nextElement();
 109             check(! entries.hasMoreElements());
 110             checkZipEntry(ze, contents(zf, ze));


 111         }
 112 
 113         try (FileInputStream fis = new FileInputStream(f);
 114              ZipInputStream is = new ZipInputStream(fis))
 115         {
 116             ze = is.getNextEntry();
 117             checkZipEntry(ze, contents(is));
 118             check(is.getNextEntry() == null);


 119         }
 120         f.delete();
 121         System.out.printf("passed = %d, failed = %d%n", passed, failed);
 122         if (failed > 0) throw new Exception("Some tests failed");
 123     }
 124 }