1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 4241361 4842702 4985614 6646605 5032358 6923692
  26    @summary Make sure we can read a zip file.
  27    @key randomness
  28  */
  29 
  30 import java.io.*;
  31 import java.nio.file.Files;
  32 import java.nio.file.Paths;
  33 import java.nio.file.StandardCopyOption;
  34 import java.nio.file.StandardOpenOption;
  35 import java.util.zip.*;
  36 
  37 public class ReadZip {
  38     private static void unreached (Object o)
  39         throws Exception
  40     {
  41         // Should never get here
  42         throw new Exception ("Expected exception was not thrown");
  43     }
  44 
  45     public static void main(String args[]) throws Exception {
  46         try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
  47                                                "input.zip"))) {
  48             // Make sure we throw NPE on null objects
  49             try { unreached (zf.getEntry(null)); }
  50             catch (NullPointerException e) {}
  51 
  52             try { unreached (zf.getInputStream(null)); }
  53             catch (NullPointerException e) {}
  54 
  55             ZipEntry ze = zf.getEntry("ReadZip.java");
  56             if (ze == null) {
  57                 throw new Exception("cannot read from zip file");
  58             }
  59         }
  60 
  61         // Make sure we can read the zip file that has some garbage
  62         // bytes padded at the end.
  63         File newZip = new File(System.getProperty("test.dir", "."), "input2.zip");
  64         Files.copy(Paths.get(System.getProperty("test.src", ""), "input.zip"),
  65                    newZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
  66 
  67         newZip.setWritable(true);
  68 
  69         // pad some bytes
  70         try (OutputStream os = Files.newOutputStream(newZip.toPath(),
  71                                                      StandardOpenOption.APPEND)) {
  72             os.write(1); os.write(3); os.write(5); os.write(7);
  73         }
  74 
  75         try (ZipFile zf = new ZipFile(newZip)) {
  76             ZipEntry ze = zf.getEntry("ReadZip.java");
  77             if (ze == null) {
  78                 throw new Exception("cannot read from zip file");
  79             }
  80         } finally {
  81             newZip.delete();
  82         }
  83 
  84         // Read zip file comment
  85         try {
  86             try (FileOutputStream fos = new FileOutputStream(newZip);
  87                  ZipOutputStream zos = new ZipOutputStream(fos))
  88             {
  89                 ZipEntry ze = new ZipEntry("ZipEntry");
  90                 zos.putNextEntry(ze);
  91                 zos.write(1); zos.write(2); zos.write(3); zos.write(4);
  92                 zos.closeEntry();
  93                 zos.setComment("This is the comment for testing");
  94             }
  95 
  96             try (ZipFile zf = new ZipFile(newZip)) {
  97                 ZipEntry ze = zf.getEntry("ZipEntry");
  98                 if (ze == null)
  99                     throw new Exception("cannot read entry from zip file");
 100                 if (!"This is the comment for testing".equals(zf.getComment()))
 101                     throw new Exception("cannot read comment from zip file");
 102             }
 103         } finally {
 104             newZip.delete();
 105         }
 106 
 107         // Throw a FNF exception when read a non-existing zip file
 108         try { unreached (new ZipFile(
 109                              new File(System.getProperty("test.src", "."),
 110                                      "input"
 111                                       + String.valueOf(new java.util.Random().nextInt())
 112                                       + ".zip")));
 113         } catch (FileNotFoundException fnfe) {}
 114     }
 115 }