author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8553 | 46c2babb1e44 |
child 22948 | 2cf7e7202386 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8553
diff
changeset
|
2 |
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
4822
bcc6dbec57d8
6923692: java/classes_util TEST_BUG:ReadZip.java fails when
sherman
parents:
3078
diff
changeset
|
25 |
@bug 4241361 4842702 4985614 6646605 5032358 6923692 |
2 | 26 |
@summary Make sure we can read a zip file. |
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
30 |
import java.nio.file.Files; |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
31 |
import java.nio.file.Paths; |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
32 |
import java.nio.file.StandardCopyOption; |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
33 |
import java.nio.file.StandardOpenOption; |
2 | 34 |
import java.util.zip.*; |
35 |
||
36 |
public class ReadZip { |
|
3078 | 37 |
private static void unreached (Object o) |
2 | 38 |
throws Exception |
39 |
{ |
|
40 |
// Should never get here |
|
41 |
throw new Exception ("Expected exception was not thrown"); |
|
42 |
} |
|
43 |
||
44 |
public static void main(String args[]) throws Exception { |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
45 |
try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
46 |
"input.zip"))) { |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
47 |
// Make sure we throw NPE on null objects |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
48 |
try { unreached (zf.getEntry(null)); } |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
49 |
catch (NullPointerException e) {} |
2 | 50 |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
51 |
try { unreached (zf.getInputStream(null)); } |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
52 |
catch (NullPointerException e) {} |
2 | 53 |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
54 |
ZipEntry ze = zf.getEntry("ReadZip.java"); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
55 |
if (ze == null) { |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
56 |
throw new Exception("cannot read from zip file"); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
57 |
} |
2 | 58 |
} |
3078 | 59 |
|
60 |
// Make sure we can read the zip file that has some garbage |
|
61 |
// bytes padded at the end. |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
62 |
File newZip = new File(System.getProperty("test.dir", "."), "input2.zip"); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
63 |
Files.copy(Paths.get(System.getProperty("test.src", ""), "input.zip"), |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
64 |
newZip.toPath(), StandardCopyOption.REPLACE_EXISTING); |
3078 | 65 |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
66 |
// pad some bytes |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
67 |
try (OutputStream os = Files.newOutputStream(newZip.toPath(), |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
68 |
StandardOpenOption.APPEND)) { |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
69 |
os.write(1); os.write(3); os.write(5); os.write(7); |
3078 | 70 |
} |
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
71 |
|
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
72 |
try (ZipFile zf = new ZipFile(newZip)) { |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
73 |
ZipEntry ze = zf.getEntry("ReadZip.java"); |
3078 | 74 |
if (ze == null) { |
75 |
throw new Exception("cannot read from zip file"); |
|
76 |
} |
|
77 |
} finally { |
|
78 |
newZip.delete(); |
|
79 |
} |
|
80 |
||
81 |
// Read zip file comment |
|
82 |
try { |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
83 |
try (FileOutputStream fos = new FileOutputStream(newZip); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
84 |
ZipOutputStream zos = new ZipOutputStream(fos)) |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
85 |
{ |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
86 |
ZipEntry ze = new ZipEntry("ZipEntry"); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
87 |
zos.putNextEntry(ze); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
88 |
zos.write(1); zos.write(2); zos.write(3); zos.write(4); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
89 |
zos.closeEntry(); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
90 |
zos.setComment("This is the comment for testing"); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
91 |
} |
3078 | 92 |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
93 |
try (ZipFile zf = new ZipFile(newZip)) { |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
94 |
ZipEntry ze = zf.getEntry("ZipEntry"); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
95 |
if (ze == null) |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
96 |
throw new Exception("cannot read entry from zip file"); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
97 |
if (!"This is the comment for testing".equals(zf.getComment())) |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
98 |
throw new Exception("cannot read comment from zip file"); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
7668
diff
changeset
|
99 |
} |
3078 | 100 |
} finally { |
101 |
newZip.delete(); |
|
102 |
} |
|
103 |
||
104 |
// Throw a FNF exception when read a non-existing zip file |
|
105 |
try { unreached (new ZipFile( |
|
106 |
new File(System.getProperty("test.src", "."), |
|
107 |
"input" |
|
108 |
+ String.valueOf(new java.util.Random().nextInt()) |
|
109 |
+ ".zip"))); |
|
110 |
} catch (FileNotFoundException fnfe) {} |
|
2 | 111 |
} |
112 |
} |