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