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 6233323 8144977
  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.NoSuchFileException;
  34 import java.nio.file.StandardCopyOption;
  35 import java.nio.file.StandardOpenOption;


  36 import java.util.zip.*;
  37 


  38 public class ReadZip {
  39     private static void unreached (Object o)
  40         throws Exception
  41     {
  42         // Should never get here
  43         throw new Exception ("Expected exception was not thrown");
  44     }
  45 
  46     public static void main(String args[]) throws Exception {
  47         try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
  48                                                "input.zip"))) {
  49             // Make sure we throw NPE on null objects
  50             try { unreached (zf.getEntry(null)); }
  51             catch (NullPointerException e) {}
  52 
  53             try { unreached (zf.getInputStream(null)); }
  54             catch (NullPointerException e) {}
  55 
  56             ZipEntry ze = zf.getEntry("ReadZip.java");
  57             if (ze == null) {
  58                 throw new Exception("cannot read from zip file");
  59             }
  60         }
  61 
  62         // Make sure we can read the zip file that has some garbage
  63         // bytes padded at the end.
  64         File newZip = new File(System.getProperty("test.dir", "."), "input2.zip");
  65         Files.copy(Paths.get(System.getProperty("test.src", ""), "input.zip"),
  66                    newZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
  67 
  68         newZip.setWritable(true);
  69 
  70         // pad some bytes
  71         try (OutputStream os = Files.newOutputStream(newZip.toPath(),
  72                                                      StandardOpenOption.APPEND)) {
  73             os.write(1); os.write(3); os.write(5); os.write(7);
  74         }
  75 
  76         try (ZipFile zf = new ZipFile(newZip)) {
  77             ZipEntry ze = zf.getEntry("ReadZip.java");
  78             if (ze == null) {
  79                 throw new Exception("cannot read from zip file");
  80             }
  81         } finally {
  82             newZip.delete();
  83         }
  84 
  85         // Read zip file comment
  86         try {
  87             try (FileOutputStream fos = new FileOutputStream(newZip);
  88                  ZipOutputStream zos = new ZipOutputStream(fos))
  89             {
  90                 ZipEntry ze = new ZipEntry("ZipEntry");
  91                 zos.putNextEntry(ze);
  92                 zos.write(1); zos.write(2); zos.write(3); zos.write(4);
  93                 zos.closeEntry();
  94                 zos.setComment("This is the comment for testing");
  95             }
  96 
  97             try (ZipFile zf = new ZipFile(newZip)) {
  98                 ZipEntry ze = zf.getEntry("ZipEntry");
  99                 if (ze == null)
 100                     throw new Exception("cannot read entry from zip file");
 101                 if (!"This is the comment for testing".equals(zf.getComment()))
 102                     throw new Exception("cannot read comment from zip file");
 103             }
 104         } finally {
 105             newZip.delete();
 106         }
 107 
 108         // Read directory entry
 109         try {
 110             try (FileOutputStream fos = new FileOutputStream(newZip);
 111                  ZipOutputStream zos = new ZipOutputStream(fos))
 112             {
 113                 ZipEntry ze = new ZipEntry("directory/");
 114                 zos.putNextEntry(ze);
 115                 zos.closeEntry();
 116             }
 117             try (ZipFile zf = new ZipFile(newZip)) {
 118                 ZipEntry ze = zf.getEntry("directory/");
 119                 if (ze == null || !ze.isDirectory())
 120                     throw new RuntimeException("read entry \"directory/\" failed");
 121                 try (InputStream is = zf.getInputStream(ze)) {
 122                     is.available();
 123                 } catch (Exception x) {
 124                     x.printStackTrace();
 125                 }
 126 
 127                 ze = zf.getEntry("directory");
 128                 if (ze == null || !ze.isDirectory())
 129                     throw new RuntimeException("read entry \"directory\" failed");
 130                 try (InputStream is = zf.getInputStream(ze)) {
 131                     is.available();
 132                 } catch (Exception x) {
 133                     x.printStackTrace();
 134                 }
 135             }
 136         } finally {
 137             newZip.delete();
 138         }
 139 
 140 
 141 
 142         // Throw a FNF exception when read a non-existing zip file
 143         try { unreached (new ZipFile(
 144                              new File(System.getProperty("test.src", "."),
 145                                      "input"
 146                                       + String.valueOf(new java.util.Random().nextInt())
 147                                       + ".zip")));
 148         } catch (NoSuchFileException nsfe) {}

















































 149     }
 150 }
--- EOF ---