test/java/util/zip/ZipFile/ReadZip.java

Print this page




  46                                                "input.zip"))) {
  47             // Make sure we throw NPE on null objects
  48             try { unreached (zf.getEntry(null)); }
  49             catch (NullPointerException e) {}
  50 
  51             try { unreached (zf.getInputStream(null)); }
  52             catch (NullPointerException e) {}
  53 
  54             ZipEntry ze = zf.getEntry("ReadZip.java");
  55             if (ze == null) {
  56                 throw new Exception("cannot read from zip file");
  57             }
  58         }
  59 
  60         // Make sure we can read the zip file that has some garbage
  61         // bytes padded at the end.
  62         File newZip = new File(System.getProperty("test.dir", "."), "input2.zip");
  63         Files.copy(Paths.get(System.getProperty("test.src", ""), "input.zip"),
  64                    newZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
  65 


  66         // pad some bytes
  67         try (OutputStream os = Files.newOutputStream(newZip.toPath(),
  68                                                      StandardOpenOption.APPEND)) {
  69             os.write(1); os.write(3); os.write(5); os.write(7);
  70         }
  71 
  72         try (ZipFile zf = new ZipFile(newZip)) {
  73             ZipEntry ze = zf.getEntry("ReadZip.java");
  74             if (ze == null) {
  75                 throw new Exception("cannot read from zip file");
  76             }
  77         } finally {
  78             newZip.delete();
  79         }
  80 
  81         // Read zip file comment
  82         try {
  83             try (FileOutputStream fos = new FileOutputStream(newZip);
  84                  ZipOutputStream zos = new ZipOutputStream(fos))
  85             {




  46                                                "input.zip"))) {
  47             // Make sure we throw NPE on null objects
  48             try { unreached (zf.getEntry(null)); }
  49             catch (NullPointerException e) {}
  50 
  51             try { unreached (zf.getInputStream(null)); }
  52             catch (NullPointerException e) {}
  53 
  54             ZipEntry ze = zf.getEntry("ReadZip.java");
  55             if (ze == null) {
  56                 throw new Exception("cannot read from zip file");
  57             }
  58         }
  59 
  60         // Make sure we can read the zip file that has some garbage
  61         // bytes padded at the end.
  62         File newZip = new File(System.getProperty("test.dir", "."), "input2.zip");
  63         Files.copy(Paths.get(System.getProperty("test.src", ""), "input.zip"),
  64                    newZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
  65 
  66         newZip.setWritable(true);
  67 
  68         // pad some bytes
  69         try (OutputStream os = Files.newOutputStream(newZip.toPath(),
  70                                                      StandardOpenOption.APPEND)) {
  71             os.write(1); os.write(3); os.write(5); os.write(7);
  72         }
  73 
  74         try (ZipFile zf = new ZipFile(newZip)) {
  75             ZipEntry ze = zf.getEntry("ReadZip.java");
  76             if (ze == null) {
  77                 throw new Exception("cannot read from zip file");
  78             }
  79         } finally {
  80             newZip.delete();
  81         }
  82 
  83         // Read zip file comment
  84         try {
  85             try (FileOutputStream fos = new FileOutputStream(newZip);
  86                  ZipOutputStream zos = new ZipOutputStream(fos))
  87             {