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


 148         // Throw a FNF exception when read a non-existing zip file
 149         try { unreached (new ZipFile(
 150                              new File(System.getProperty("test.src", "."),
 151                                      "input"
 152                                       + String.valueOf(new java.util.Random().nextInt())
 153                                       + ".zip")));
 154         } catch (NoSuchFileException nsfe) {}
 155 
 156         // read a zip file with ZIP64 end 
 157         Path path = Paths.get(System.getProperty("test.dir", ""), "end64.zip");
 158         try {
 159             URI uri = URI.create("jar:" + path.toUri());
 160             Map<String, Object> env = Map.of("create", "true", "forceZIP64End", "true");
 161             try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
 162                 Files.write(fs.getPath("hello"), "hello".getBytes());
 163             }
 164             try (ZipFile zf = new ZipFile(path.toFile())) {
 165                 if (!"hello".equals(new String(zf.getInputStream(new ZipEntry("hello"))
 166                                                .readAllBytes(),
 167                                                US_ASCII)))
 168                     throw new RuntimeException("zipfile: read entry failed");
 169             } catch (IOException x) {
 170                 throw new RuntimeException("zipfile: zip64 end failed");
 171             }
 172             try (FileSystem fs = FileSystems.newFileSystem(uri, Map.of())) {
 173                 if (!"hello".equals(new String(Files.readAllBytes(fs.getPath("hello")))))
 174                     throw new RuntimeException("zipfs: read entry failed");
 175             } catch (IOException x) {
 176                 throw new RuntimeException("zipfile: zip64 end failed");
 177             }
 178         } finally {
 179             Files.deleteIfExists(path);
 180         }
 181 
 182         // read a zip file created via "echo hello | zip dst.zip -", which uses
 183         // ZIP64 end record
 184         if (Files.notExists(Paths.get("/usr/bin/zip")))
 185             return;
 186         try {
 187             Process zip = new ProcessBuilder("zip", path.toString().toString(), "-").start();
 188             OutputStream os = zip.getOutputStream();
 189             os.write("hello".getBytes(US_ASCII));
 190             os.close();
 191             zip.waitFor();
 192             if (zip.exitValue() == 0 && Files.exists(path)) {
 193                 try (ZipFile zf = new ZipFile(path.toFile())) {
 194                     if (!"hello".equals(new String(zf.getInputStream(new ZipEntry("-"))
 195                                                        .readAllBytes())))
 196                         throw new RuntimeException("zipfile: read entry failed");
 197                 } catch (IOException x) {
 198                     throw new RuntimeException("zipfile: zip64 end failed");
 199                 }
 200             }
 201         } finally {
 202             Files.deleteIfExists(path);
 203         }
 204     }
 205 }
--- EOF ---