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) {
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 }
|
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) {
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 }
|