0N/A/*
3909N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
2120N/A @bug 4241361 4842702 4985614 6646605 5032358 6923692
0N/A @summary Make sure we can read a zip file.
0N/A */
0N/A
0N/Aimport java.io.*;
3656N/Aimport java.nio.file.Files;
3656N/Aimport java.nio.file.Paths;
3656N/Aimport java.nio.file.StandardCopyOption;
3656N/Aimport java.nio.file.StandardOpenOption;
0N/Aimport java.util.zip.*;
0N/A
0N/Apublic class ReadZip {
1332N/A private static void unreached (Object o)
0N/A throws Exception
0N/A {
0N/A // Should never get here
0N/A throw new Exception ("Expected exception was not thrown");
0N/A }
0N/A
0N/A public static void main(String args[]) throws Exception {
3656N/A try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
3656N/A "input.zip"))) {
3656N/A // Make sure we throw NPE on null objects
3656N/A try { unreached (zf.getEntry(null)); }
3656N/A catch (NullPointerException e) {}
0N/A
3656N/A try { unreached (zf.getInputStream(null)); }
3656N/A catch (NullPointerException e) {}
0N/A
3656N/A ZipEntry ze = zf.getEntry("ReadZip.java");
3656N/A if (ze == null) {
3656N/A throw new Exception("cannot read from zip file");
3656N/A }
0N/A }
1332N/A
1332N/A // Make sure we can read the zip file that has some garbage
1332N/A // bytes padded at the end.
3656N/A File newZip = new File(System.getProperty("test.dir", "."), "input2.zip");
3656N/A Files.copy(Paths.get(System.getProperty("test.src", ""), "input.zip"),
3656N/A newZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
1332N/A
3656N/A // pad some bytes
3656N/A try (OutputStream os = Files.newOutputStream(newZip.toPath(),
3656N/A StandardOpenOption.APPEND)) {
3656N/A os.write(1); os.write(3); os.write(5); os.write(7);
1332N/A }
3656N/A
3656N/A try (ZipFile zf = new ZipFile(newZip)) {
3656N/A ZipEntry ze = zf.getEntry("ReadZip.java");
1332N/A if (ze == null) {
1332N/A throw new Exception("cannot read from zip file");
1332N/A }
1332N/A } finally {
1332N/A newZip.delete();
1332N/A }
1332N/A
1332N/A // Read zip file comment
1332N/A try {
3656N/A try (FileOutputStream fos = new FileOutputStream(newZip);
3656N/A ZipOutputStream zos = new ZipOutputStream(fos))
3656N/A {
3656N/A ZipEntry ze = new ZipEntry("ZipEntry");
3656N/A zos.putNextEntry(ze);
3656N/A zos.write(1); zos.write(2); zos.write(3); zos.write(4);
3656N/A zos.closeEntry();
3656N/A zos.setComment("This is the comment for testing");
3656N/A }
1332N/A
3656N/A try (ZipFile zf = new ZipFile(newZip)) {
3656N/A ZipEntry ze = zf.getEntry("ZipEntry");
3656N/A if (ze == null)
3656N/A throw new Exception("cannot read entry from zip file");
3656N/A if (!"This is the comment for testing".equals(zf.getComment()))
3656N/A throw new Exception("cannot read comment from zip file");
3656N/A }
1332N/A } finally {
1332N/A newZip.delete();
1332N/A }
1332N/A
1332N/A // Throw a FNF exception when read a non-existing zip file
1332N/A try { unreached (new ZipFile(
1332N/A new File(System.getProperty("test.src", "."),
1332N/A "input"
1332N/A + String.valueOf(new java.util.Random().nextInt())
1332N/A + ".zip")));
1332N/A } catch (FileNotFoundException fnfe) {}
0N/A }
0N/A}