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