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
26 @summary Make sure we can read a zip file.
27 */
28
29 import java.io.*;
30 import java.util.zip.*;
31
32 public class ReadZip {
33 private static void unreached (Object o)
34 throws Exception
35 {
36 // Should never get here
37 throw new Exception ("Expected exception was not thrown");
38 }
39
40 public static void main(String args[]) throws Exception {
41 ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
42 "input.zip"));
43
44 // Make sure we throw NPE on null objects
45 try { unreached (zf.getEntry(null)); }
46 catch (NullPointerException e) {}
47
48 try { unreached (zf.getInputStream(null)); }
49 catch (NullPointerException e) {}
50
51 ZipEntry ze = zf.getEntry("ReadZip.java");
52 if (ze == null) {
53 throw new Exception("cannot read from zip file");
54 }
55 zf.close();
56
57 // Make sure we can read the zip file that has some garbage
58 // bytes padded at the end.
59 FileInputStream fis = new FileInputStream(
60 new File(System.getProperty("test.src", "."),
61 "input.zip"));
62 File newZip = new File(System.getProperty("test.dir", "."),
63 "input2.zip");
64 FileOutputStream fos = new FileOutputStream(newZip);
65
66 byte[] buf = new byte[1024];
67 int n = 0;
68 while ((n = fis.read(buf)) != -1) {
69 fos.write(buf, 0, n);
70 }
71 fis.close();
72 // pad some bytes
73 fos.write(1); fos.write(3); fos.write(5); fos.write(7);
74 fos.close();
75 try {
76 zf = new ZipFile(newZip);
77 ze = zf.getEntry("ReadZip.java");
78 if (ze == null) {
79 throw new Exception("cannot read from zip file");
80 }
81 } finally {
82 zf.close();
83 newZip.delete();
84 }
85
86 // Read zip file comment
87 try {
88
89 ZipOutputStream zos = new ZipOutputStream(
90 new FileOutputStream(newZip));
91 ze = new ZipEntry("ZipEntry");
92 zos.putNextEntry(ze);
93 zos.write(1); zos.write(2); zos.write(3); zos.write(4);
94 zos.closeEntry();
95 zos.setComment("This is the comment for testing");
96 zos.close();
97
98 zf = new ZipFile(newZip);
99 ze = zf.getEntry("ZipEntry");
100 if (ze == null)
101 throw new Exception("cannot read entry from zip file");
102 if (!"This is the comment for testing".equals(zf.getComment()))
103 throw new Exception("cannot read comment from zip file");
104 } finally {
105 zf.close();
106 newZip.delete();
107 }
108
109 // Throw a FNF exception when read a non-existing zip file
110 try { unreached (new ZipFile(
111 new File(System.getProperty("test.src", "."),
112 "input"
113 + String.valueOf(new java.util.Random().nextInt())
114 + ".zip")));
115 } catch (FileNotFoundException fnfe) {}
116 }
117 }
|
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
26 @summary Make sure we can read a zip file.
27 */
28
29 import java.io.*;
30 import java.nio.file.Files;
31 import java.nio.file.StandardCopyOption;
32 import java.nio.file.StandardOpenOption;
33 import java.util.zip.*;
34
35 public class ReadZip {
36 private static void unreached (Object o)
37 throws Exception
38 {
39 // Should never get here
40 throw new Exception ("Expected exception was not thrown");
41 }
42
43 public static void main(String args[]) throws Exception {
44 try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
45 "input.zip"))) {
46 // Make sure we throw NPE on null objects
47 try { unreached (zf.getEntry(null)); }
48 catch (NullPointerException e) {}
49
50 try { unreached (zf.getInputStream(null)); }
51 catch (NullPointerException e) {}
52
53 ZipEntry ze = zf.getEntry("ReadZip.java");
54 if (ze == null) {
55 throw new Exception("cannot read from zip file");
56 }
57 }
58
59 // Make sure we can read the zip file that has some garbage
60 // bytes padded at the end.
61 File newZip = new File(System.getProperty("test.dir", "."), "input2.zip");
62 Files.copy(new File(System.getProperty("test.src", "."), "input.zip").toPath(),
63 newZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
64
65 // pad some bytes
66 try (OutputStream os = Files.newOutputStream(newZip.toPath(),
67 StandardOpenOption.APPEND)) {
68 os.write(1); os.write(3); os.write(5); os.write(7);
69 }
70
71 try (ZipFile zf = new ZipFile(newZip)) {
72 ZipEntry ze = zf.getEntry("ReadZip.java");
73 if (ze == null) {
74 throw new Exception("cannot read from zip file");
75 }
76 } finally {
77 newZip.delete();
78 }
79
80 // Read zip file comment
81 try {
82 try (FileOutputStream fos = new FileOutputStream(newZip);
83 ZipOutputStream zos = new ZipOutputStream(fos))
84 {
85 ZipEntry ze = new ZipEntry("ZipEntry");
86 zos.putNextEntry(ze);
87 zos.write(1); zos.write(2); zos.write(3); zos.write(4);
88 zos.closeEntry();
89 zos.setComment("This is the comment for testing");
90 }
91
92 try (ZipFile zf = new ZipFile(newZip)) {
93 ZipEntry ze = zf.getEntry("ZipEntry");
94 if (ze == null)
95 throw new Exception("cannot read entry from zip file");
96 if (!"This is the comment for testing".equals(zf.getComment()))
97 throw new Exception("cannot read comment from zip file");
98 }
99 } finally {
100 newZip.delete();
101 }
102
103 // Throw a FNF exception when read a non-existing zip file
104 try { unreached (new ZipFile(
105 new File(System.getProperty("test.src", "."),
106 "input"
107 + String.valueOf(new java.util.Random().nextInt())
108 + ".zip")));
109 } catch (FileNotFoundException fnfe) {}
110 }
111 }
|