jdk/src/share/classes/java/util/zip/ZipFile.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7816 55a18147b4bf
child 9281 41e796d1b6c1
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7816
diff changeset
     2
 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
    28
import java.io.Closeable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.EOFException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    33
import java.nio.charset.Charset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Enumeration;
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
    36
import java.util.Set;
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
    37
import java.util.HashSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.NoSuchElementException;
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    39
import java.security.AccessController;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    40
import sun.security.action.GetPropertyAction;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    41
import static java.util.zip.ZipConstants64.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class is used to read entries from a zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * or method in this class will cause a {@link NullPointerException} to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
    53
class ZipFile implements ZipConstants, Closeable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private long jzfile;  // address of jzfile data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String name;  // zip file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int total;    // total number of entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean closeRequested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Mode flag to open a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static final int OPEN_READ = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Mode flag to open a zip file and mark it for deletion.  The file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * deleted some time between the moment that it is opened and the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * that it is closed, but its contents will remain accessible via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <tt>ZipFile</tt> object until either the close method is invoked or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * virtual machine exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static final int OPEN_DELETE = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        /* Zip library is loaded from System.initializeSystemClass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    83
    private static final boolean usemmap;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    84
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    85
    static {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    86
        // A system prpperty to disable mmap use to avoid vm crash when
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    87
        // in-use zip file is accidently overwritten by others.
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
    88
        String prop = sun.misc.VM.getSavedProperty("sun.zip.disableMemoryMapping");
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    89
        usemmap = (prop == null ||
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    90
                   !(prop.length() == 0 || prop.equalsIgnoreCase("true")));
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    91
    }
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    92
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Opens a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    96
     * <p>First, if there is a security manager, its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    97
     * method is called with the <code>name</code> argument as its argument
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    98
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    99
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   100
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   101
     * decode the entry names and comments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param name the name of the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *         <code>checkRead</code> method doesn't allow read access to the file.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   108
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public ZipFile(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this(new File(name), OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Opens a new <code>ZipFile</code> to read from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>File</code> object in the specified mode.  The mode argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>First, if there is a security manager, its <code>checkRead</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * method is called with the <code>name</code> argument as its argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * ensure the read is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   124
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   125
     * decode the entry names and comments
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   126
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param file the ZIP file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param mode the mode in which the file is to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws SecurityException if a security manager exists and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *         its <code>checkRead</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         doesn't allow read access to the file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *         or its <code>checkDelete</code> method doesn't allow deleting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         the file when the <tt>OPEN_DELETE</tt> flag is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public ZipFile(File file, int mode) throws IOException {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   141
        this(file, mode, Charset.forName("UTF-8"));
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   142
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   143
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   144
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   145
     * Opens a ZIP file for reading given the specified File object.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   146
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   147
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   148
     * decode the entry names and comments.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   149
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   150
     * @param file the ZIP file to be opened for reading
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   151
     * @throws ZipException if a ZIP format error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   152
     * @throws IOException if an I/O error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   153
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   154
    public ZipFile(File file) throws ZipException, IOException {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   155
        this(file, OPEN_READ);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   156
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   157
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   158
    private ZipCoder zc;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   159
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   160
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   161
     * Opens a new <code>ZipFile</code> to read from the specified
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   162
     * <code>File</code> object in the specified mode.  The mode argument
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   163
     * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   164
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   165
     * <p>First, if there is a security manager, its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   166
     * method is called with the <code>name</code> argument as its argument to
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   167
     * ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   168
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   169
     * @param file the ZIP file to be opened for reading
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   170
     * @param mode the mode in which the file is to be opened
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   171
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   172
     *        the {@linkplain java.nio.charset.Charset charset} to
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   173
     *        be used to decode the ZIP entry name and comment that are not
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   174
     *        encoded by using UTF-8 encoding (indicated by entry's general
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   175
     *        purpose flag).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   176
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   177
     * @throws ZipException if a ZIP format error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   178
     * @throws IOException if an I/O error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   179
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   180
     * @throws SecurityException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   181
     *         if a security manager exists and its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   182
     *         method doesn't allow read access to the file,or its
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   183
     *         <code>checkDelete</code> method doesn't allow deleting the
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   184
     *         file when the <tt>OPEN_DELETE</tt> flag is set
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   185
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   186
     * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   187
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   188
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   189
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   190
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   191
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   192
    public ZipFile(File file, int mode, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   193
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (((mode & OPEN_READ) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new IllegalArgumentException("Illegal mode: 0x"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                               Integer.toHexString(mode));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        String name = file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            sm.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if ((mode & OPEN_DELETE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                sm.checkDelete(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   207
        if (charset == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   208
            throw new NullPointerException("charset is null");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   209
        this.zc = ZipCoder.get(charset);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   210
        long t0 = System.nanoTime();
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
   211
        jzfile = open(name, mode, file.lastModified(), usemmap);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   212
        sun.misc.PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   213
        sun.misc.PerfCounter.getZipFileCount().increment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.total = getTotal(jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   218
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   219
     * Opens a zip file for reading.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   220
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   221
     * <p>First, if there is a security manager, its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   222
     * method is called with the <code>name</code> argument as its argument
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   223
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   224
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   225
     * @param name the name of the zip file
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   226
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   227
     *        the {@linkplain java.nio.charset.Charset charset} to
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   228
     *        be used to decode the ZIP entry name and comment that are not
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   229
     *        encoded by using UTF-8 encoding (indicated by entry's general
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   230
     *        purpose flag).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   231
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   232
     * @throws ZipException if a ZIP format error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   233
     * @throws IOException if an I/O error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   234
     * @throws SecurityException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   235
     *         if a security manager exists and its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   236
     *         method doesn't allow read access to the file
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   237
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   238
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   239
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   240
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   241
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   242
    public ZipFile(String name, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   243
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   244
        this(new File(name), OPEN_READ, charset);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   245
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Opens a ZIP file for reading given the specified File object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param file the ZIP file to be opened for reading
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   250
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   251
     *        The {@linkplain java.nio.charset.Charset charset} to be
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   252
     *        used to decode the ZIP entry name and comment (ignored if
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   253
     *        the <a href="package-summary.html#lang_encoding"> language
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   254
     *        encoding bit</a> of the ZIP entry's general purpose bit
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   255
     *        flag is set).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   256
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   257
     * @throws ZipException if a ZIP format error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @throws IOException if an I/O error has occurred
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   259
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   260
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   262
    public ZipFile(File file, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   263
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   264
        this(file, OPEN_READ, charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   268
     * Returns the zip file comment, or null if none.
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   269
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   270
     * @return the comment string for the zip file, or null if none
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   271
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   272
     * @throws IllegalStateException if the zip file has been closed
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   273
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   274
     * Since 1.7
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   275
     */
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   276
    public String getComment() {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   277
        synchronized (this) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   278
            ensureOpen();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   279
            byte[] bcomm = getCommentBytes(jzfile);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   280
            if (bcomm == null)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   281
                return null;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   282
            return zc.toString(bcomm, bcomm.length);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   283
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   284
    }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   285
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   286
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns the zip file entry for the specified name, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param name the name of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return the zip file entry, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public ZipEntry getEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        long jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            ensureOpen();
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   301
            jzentry = getEntry(jzfile, zc.getBytes(name), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (jzentry != 0) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   303
                ZipEntry ze = getZipEntry(name, jzentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                freeEntry(jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   311
    private static native long getEntry(long jzfile, byte[] name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                        boolean addSlash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    // freeEntry releases the C jzentry struct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static native void freeEntry(long jzfile, long jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   317
    // the outstanding inputstreams that need to be closed.
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   318
    private Set<InputStream> streams = new HashSet<>();
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   319
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <p> Closing this ZIP file will, in turn, close all input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * streams that have been returned by invocations of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param entry the zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @return the input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public InputStream getInputStream(ZipEntry entry) throws IOException {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   335
        if (entry == null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   336
            throw new NullPointerException("entry");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        long jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        ZipFileInputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            ensureOpen();
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   342
            if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   343
                jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), false);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   344
            } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   345
                jzentry = getEntry(jzfile, zc.getBytes(entry.name), false);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   346
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (jzentry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            in = new ZipFileInputStream(jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   352
            switch (getEntryMethod(jzentry)) {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   353
            case STORED:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   354
                streams.add(in);
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   355
                return in;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   356
            case DEFLATED:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   357
                final ZipFileInputStream zfin = in;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   358
                // MORE: Compute good size for inflater stream:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   359
                long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   360
                if (size > 65536) size = 8192;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   361
                if (size <= 0) size = 4096;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   362
                InputStream is = new InflaterInputStream(zfin, getInflater(), (int)size) {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   363
                    private boolean isClosed = false;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   364
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   365
                    public void close() throws IOException {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   366
                        if (!isClosed) {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   367
                            super.close();
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   368
                            releaseInflater(inf);
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   369
                            isClosed = true;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   370
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    }
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   372
                    // Override fill() method to provide an extra "dummy" byte
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   373
                    // at the end of the input stream. This is required when
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   374
                    // using the "nowrap" Inflater option.
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   375
                    protected void fill() throws IOException {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   376
                        if (eof) {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   377
                            throw new EOFException(
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   378
                                "Unexpected end of ZLIB input stream");
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   379
                        }
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   380
                        len = this.in.read(buf, 0, buf.length);
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   381
                        if (len == -1) {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   382
                            buf[0] = 0;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   383
                            len = 1;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   384
                            eof = true;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   385
                        }
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   386
                        inf.setInput(buf, 0, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    }
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   388
                    private boolean eof;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   390
                    public int available() throws IOException {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   391
                        if (isClosed)
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   392
                            return 0;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   393
                        long avail = zfin.size() - inf.getBytesWritten();
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   394
                        return avail > (long) Integer.MAX_VALUE ?
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   395
                            Integer.MAX_VALUE : (int) avail;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   396
                    }
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   397
                };
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   398
                streams.add(is);
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   399
                return is;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   400
            default:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   401
                throw new ZipException("invalid compression method");
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   402
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Gets an inflater from the list of available inflaters or allocates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private Inflater getInflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        synchronized (inflaters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            int size = inflaters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                Inflater inf = (Inflater)inflaters.remove(size - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                return inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                return new Inflater(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Releases the specified inflater to the list of available inflaters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    private void releaseInflater(Inflater inf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        synchronized (inflaters) {
7815
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 7803
diff changeset
   427
            if (inf.ended())
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents: 7803
diff changeset
   428
                return;
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
   429
            inf.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            inflaters.add(inf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    // List of available Inflater objects for decompression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private Vector inflaters = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Returns the path name of the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return the path name of the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Returns an enumeration of the ZIP file entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return an enumeration of the ZIP file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public Enumeration<? extends ZipEntry> entries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return new Enumeration<ZipEntry>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                private int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        return i < total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                public ZipEntry nextElement() throws NoSuchElementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        if (i >= total) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        long jzentry = getNextEntry(jzfile, i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        if (jzentry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            String message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                            if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                                message = "ZipFile concurrently closed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                message = getZipMessage(ZipFile.this.jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                            throw new ZipError("jzentry == 0" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                               ",\n jzfile = " + ZipFile.this.jzfile +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                               ",\n total = " + ZipFile.this.total +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                               ",\n name = " + ZipFile.this.name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                                               ",\n i = " + i +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                               ",\n message = " + message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   482
                        ZipEntry ze = getZipEntry(null, jzentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        freeEntry(jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   490
    private ZipEntry getZipEntry(String name, long jzentry) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   491
        ZipEntry e = new ZipEntry();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   492
        e.flag = getEntryFlag(jzentry);  // get the flag first
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   493
        if (name != null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   494
            e.name = name;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   495
        } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   496
            byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   497
            if (!zc.isUTF8() && (e.flag & EFS) != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   498
                e.name = zc.toStringUTF8(bname, bname.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   499
            } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   500
                e.name = zc.toString(bname, bname.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   501
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   502
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   503
        e.time = getEntryTime(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   504
        e.crc = getEntryCrc(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   505
        e.size = getEntrySize(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   506
        e. csize = getEntryCSize(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   507
        e.method = getEntryMethod(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   508
        e.extra = getEntryBytes(jzentry, JZENTRY_EXTRA);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   509
        byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   510
        if (bcomm == null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   511
            e.comment = null;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   512
        } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   513
            if (!zc.isUTF8() && (e.flag & EFS) != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   514
                e.comment = zc.toStringUTF8(bcomm, bcomm.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   515
            } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   516
                e.comment = zc.toString(bcomm, bcomm.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   517
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   518
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   519
        return e;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   520
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   521
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    private static native long getNextEntry(long jzfile, int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Returns the number of entries in the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return the number of entries in the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Closes the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <p> Closing this ZIP file will close all of the input streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * previously returned by invocations of the {@link #getInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * getInputStream} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            closeRequested = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   546
            if (streams.size() !=0) {
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   547
                Set<InputStream> copy = streams;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7550
diff changeset
   548
                streams = new HashSet<>();
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   549
                for (InputStream is: copy)
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   550
                    is.close();
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   551
            }
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   552
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if (jzfile != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                // Close the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                long zf = this.jzfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                jzfile = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                close(zf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                // Release inflaters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                synchronized (inflaters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    int size = inflaters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        Inflater inf = (Inflater)inflaters.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        inf.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Ensures that the <code>close</code> method of this ZIP file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * called when there are no more references to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Since the time when GC would invoke this method is undetermined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * it is strongly recommended that applications invoke the <code>close</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * method as soon they have finished accessing this <code>ZipFile</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * This will prevent holding up system resources for an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * length of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @see    java.util.zip.ZipFile#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    private static native void close(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            throw new IllegalStateException("zip file closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (jzfile == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            throw new IllegalStateException("The object is not initialized.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    private void ensureOpenOrZipException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            throw new ZipException("ZipFile closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Inner class implementing the input stream used to read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * (possibly compressed) zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
   private class ZipFileInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        protected long jzentry; // address of jzentry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        private   long pos;     // current position within entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        protected long rem;     // number of remaining bytes within entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        protected long size;    // uncompressed size of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        ZipFileInputStream(long jzentry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            pos = 0;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   621
            rem = getEntryCSize(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   622
            size = getEntrySize(jzentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            this.jzentry = jzentry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            if (len > rem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                len = (int) rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                ensureOpenOrZipException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                   off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                pos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                rem -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            if (read(b, 0, 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        public long skip(long n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            if (n > rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                n = rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            rem -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        public long size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            rem = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                if (jzentry != 0 && ZipFile.this.jzfile != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    freeEntry(ZipFile.this.jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                }
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   687
                streams.remove(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   692
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
   693
    private static native long open(String name, int mode, long lastModified,
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
   694
                                    boolean usemmap) throws IOException;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   695
    private static native int getTotal(long jzfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    private static native int read(long jzfile, long jzentry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                                   long pos, byte[] b, int off, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   699
    // access to the native zentry object
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   700
    private static native long getEntryTime(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   701
    private static native long getEntryCrc(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   702
    private static native long getEntryCSize(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   703
    private static native long getEntrySize(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   704
    private static native int getEntryMethod(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   705
    private static native int getEntryFlag(long jzentry);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   706
    private static native byte[] getCommentBytes(long jzfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   708
    private static final int JZENTRY_NAME = 0;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   709
    private static final int JZENTRY_EXTRA = 1;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   710
    private static final int JZENTRY_COMMENT = 2;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   711
    private static native byte[] getEntryBytes(long jzentry, int type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    private static native String getZipMessage(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
}