jdk/test/java/util/zip/ZipFile/ReadZip.java
author sherman
Sat, 06 Feb 2010 09:26:57 -0800
changeset 4822 bcc6dbec57d8
parent 3078 c491f0d2a8aa
child 5506 202f599c92aa
permissions -rw-r--r--
6923692: java/classes_util TEST_BUG:ReadZip.java fails when Summary: to create the test file at test.dir Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Make sure we can read a zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class ReadZip {
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    33
    private static void unreached (Object o)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        // Should never get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        throw new Exception ("Expected exception was not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                                          "input.zip"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        // Make sure we throw NPE on null objects
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    45
        try { unreached (zf.getEntry(null)); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        catch (NullPointerException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    48
        try { unreached (zf.getInputStream(null)); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        catch (NullPointerException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        ZipEntry ze = zf.getEntry("ReadZip.java");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (ze == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new Exception("cannot read from zip file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        zf.close();
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    56
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    57
        // Make sure we can read the zip file that has some garbage
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    58
        // bytes padded at the end.
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    59
        FileInputStream fis = new FileInputStream(
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    60
                                   new File(System.getProperty("test.src", "."),
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    61
                                            "input.zip"));
4822
bcc6dbec57d8 6923692: java/classes_util TEST_BUG:ReadZip.java fails when
sherman
parents: 3078
diff changeset
    62
        File newZip = new File(System.getProperty("test.dir", "."),
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    63
                               "input2.zip");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    64
        FileOutputStream fos = new FileOutputStream(newZip);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    65
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    66
        byte[] buf = new byte[1024];
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    67
        int n = 0;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    68
        while ((n = fis.read(buf)) != -1) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    69
            fos.write(buf, 0, n);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    70
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    71
        fis.close();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    72
        // pad some bytes
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    73
        fos.write(1); fos.write(3); fos.write(5); fos.write(7);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    74
        fos.close();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    75
        try {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    76
            zf = new ZipFile(newZip);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    77
            ze = zf.getEntry("ReadZip.java");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    78
            if (ze == null) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    79
                throw new Exception("cannot read from zip file");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    80
            }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    81
        } finally {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    82
            zf.close();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    83
            newZip.delete();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    84
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    85
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    86
        // Read zip file comment
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    87
        try {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    88
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    89
            ZipOutputStream zos = new ZipOutputStream(
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    90
                                      new FileOutputStream(newZip));
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    91
            ze = new ZipEntry("ZipEntry");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    92
            zos.putNextEntry(ze);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    93
            zos.write(1); zos.write(2); zos.write(3); zos.write(4);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    94
            zos.closeEntry();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    95
            zos.setComment("This is the comment for testing");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    96
            zos.close();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    97
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    98
            zf = new ZipFile(newZip);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    99
            ze = zf.getEntry("ZipEntry");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   100
            if (ze == null)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   101
                throw new Exception("cannot read entry from zip file");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   102
            if (!"This is the comment for testing".equals(zf.getComment()))
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   103
                throw new Exception("cannot read comment from zip file");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   104
        } finally {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   105
            zf.close();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   106
            newZip.delete();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   107
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   108
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   109
        // Throw a FNF exception when read a non-existing zip file
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   110
        try { unreached (new ZipFile(
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   111
                             new File(System.getProperty("test.src", "."),
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   112
                                     "input"
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   113
                                      + String.valueOf(new java.util.Random().nextInt())
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   114
                                      + ".zip")));
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   115
        } catch (FileNotFoundException fnfe) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}