jdk/src/share/classes/java/util/zip/ZipFile.java
author sherman
Thu, 11 Mar 2010 14:06:09 -0800
changeset 5148 2bf5469864ea
parent 3849 34469964aa98
child 5506 202f599c92aa
permissions -rw-r--r--
6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile Summary: system property sun.zip.disableMemoryMapping to disable mmap use 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;
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.
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    88
        String prop = AccessController.doPrivileged(
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    89
            new GetPropertyAction("sun.zip.disableMemoryMapping"));
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    90
        usemmap = (prop == null ||
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    91
                   !(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
    92
    }
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    93
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Opens a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    97
     * <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
    98
     * 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
    99
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   100
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   101
     * <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
   102
     * decode the entry names and comments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param name the name of the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *         <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
   109
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public ZipFile(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this(new File(name), OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Opens a new <code>ZipFile</code> to read from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <code>File</code> object in the specified mode.  The mode argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>First, if there is a security manager, its <code>checkRead</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * method is called with the <code>name</code> argument as its argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * ensure the read is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   125
     * <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
   126
     * decode the entry names and comments
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   127
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param file the ZIP file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param mode the mode in which the file is to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws SecurityException if a security manager exists and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         its <code>checkRead</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *         doesn't allow read access to the file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         or its <code>checkDelete</code> method doesn't allow deleting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *         the file when the <tt>OPEN_DELETE</tt> flag is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    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
   142
        this(file, mode, Charset.forName("UTF-8"));
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
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   146
     * 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
   147
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   148
     * <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
   149
     * decode the entry names and comments.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   150
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   151
     * @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
   152
     * @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
   153
     * @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
   154
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   155
    public ZipFile(File file) throws ZipException, IOException {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   156
        this(file, OPEN_READ);
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
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   159
    private ZipCoder zc;
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
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   162
     * 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
   163
     * <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
   164
     * 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
   165
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   166
     * <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
   167
     * 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
   168
     * ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   169
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   170
     * @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
   171
     * @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
   172
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   173
     *        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
   174
     *        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
   175
     *        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
   176
     *        purpose flag).
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
     * @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
   179
     * @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
   180
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   181
     * @throws SecurityException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   182
     *         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
   183
     *         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
   184
     *         <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
   185
     *         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
   186
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   187
     * @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
   188
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   189
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   190
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   191
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   192
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   193
    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
   194
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (((mode & OPEN_READ) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new IllegalArgumentException("Illegal mode: 0x"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                               Integer.toHexString(mode));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        String name = file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            sm.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if ((mode & OPEN_DELETE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                sm.checkDelete(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   208
        if (charset == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   209
            throw new NullPointerException("charset is null");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   210
        this.zc = ZipCoder.get(charset);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   211
        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
   212
        jzfile = open(name, mode, file.lastModified(), usemmap);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   213
        sun.misc.PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   214
        sun.misc.PerfCounter.getZipFileCount().increment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        this.total = getTotal(jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   219
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   220
     * Opens a zip file for reading.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   221
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   222
     * <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
   223
     * 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
   224
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   225
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   226
     * @param name the name of the zip file
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   227
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   228
     *        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
   229
     *        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
   230
     *        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
   231
     *        purpose flag).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   232
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   233
     * @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
   234
     * @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
   235
     * @throws SecurityException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   236
     *         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
   237
     *         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
   238
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   239
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   240
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   241
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   242
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   243
    public ZipFile(String name, Charset charset) throws IOException
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
        this(new File(name), OPEN_READ, charset);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   246
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Opens a ZIP file for reading given the specified File object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @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
   251
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   252
     *        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
   253
     *        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
   254
     *        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
   255
     *        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
   256
     *        flag is set).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   257
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   258
     * @throws ZipException if a ZIP format error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @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
   260
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   261
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   263
    public ZipFile(File file, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   264
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   265
        this(file, OPEN_READ, charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   269
     * Returns the zip file comment, or null if none.
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
     * @return the comment string for the zip file, or null if none
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   272
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   273
     * @throws IllegalStateException if the zip file has been closed
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   274
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   275
     * Since 1.7
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   276
     */
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   277
    public String getComment() {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   278
        synchronized (this) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   279
            ensureOpen();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   280
            byte[] bcomm = getCommentBytes(jzfile);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   281
            if (bcomm == null)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   282
                return null;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   283
            return zc.toString(bcomm, bcomm.length);
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
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   287
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Returns the zip file entry for the specified name, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param name the name of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return the zip file entry, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public ZipEntry getEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        long jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            ensureOpen();
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   302
            jzentry = getEntry(jzfile, zc.getBytes(name), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (jzentry != 0) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   304
                ZipEntry ze = getZipEntry(name, jzentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                freeEntry(jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   312
    private static native long getEntry(long jzfile, byte[] name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                        boolean addSlash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    // freeEntry releases the C jzentry struct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private static native void freeEntry(long jzfile, long jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
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
   318
    // 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
   319
    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
   320
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p> Closing this ZIP file will, in turn, close all input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * streams that have been returned by invocations of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param entry the zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @return the input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public InputStream getInputStream(ZipEntry entry) throws IOException {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   336
        if (entry == null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   337
            throw new NullPointerException("entry");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        long jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        ZipFileInputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            ensureOpen();
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   343
            if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   344
                jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), false);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   345
            } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   346
                jzentry = getEntry(jzfile, zc.getBytes(entry.name), false);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   347
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (jzentry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            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
   352
            streams.add(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        final ZipFileInputStream zfin = in;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   355
        switch (getEntryMethod(jzentry)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return zfin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // MORE: Compute good size for inflater stream:
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   360
            long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (size > 65536) size = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (size <= 0) size = 4096;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return new InflaterInputStream(zfin, getInflater(), (int)size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                private boolean isClosed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    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
   368
                        releaseInflater(inf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        this.in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                // Override fill() method to provide an extra "dummy" byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                // at the end of the input stream. This is required when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                // using the "nowrap" Inflater option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                protected void fill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        throw new EOFException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                            "Unexpected end of ZLIB input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    len = this.in.read(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        buf[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        len = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    inf.setInput(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                private boolean eof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    if (isClosed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    long avail = zfin.size() - inf.getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    return avail > (long) Integer.MAX_VALUE ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        Integer.MAX_VALUE : (int) avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw new ZipException("invalid compression method");
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
     * Gets an inflater from the list of available inflaters or allocates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private Inflater getInflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        synchronized (inflaters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            int size = inflaters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                Inflater inf = (Inflater)inflaters.remove(size - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                return inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                return new Inflater(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
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
     * Releases the specified inflater to the list of available inflaters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    private void releaseInflater(Inflater inf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        synchronized (inflaters) {
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
   425
            inf.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            inflaters.add(inf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    // List of available Inflater objects for decompression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    private Vector inflaters = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Returns the path name of the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @return the path name of the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Returns an enumeration of the ZIP file entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return an enumeration of the ZIP file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public Enumeration<? extends ZipEntry> entries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return new Enumeration<ZipEntry>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                private int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        return i < total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                public ZipEntry nextElement() throws NoSuchElementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        if (i >= total) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        long jzentry = getNextEntry(jzfile, i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        if (jzentry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            String message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                            if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                message = "ZipFile concurrently closed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                message = getZipMessage(ZipFile.this.jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                            throw new ZipError("jzentry == 0" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                                               ",\n jzfile = " + ZipFile.this.jzfile +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                               ",\n total = " + ZipFile.this.total +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                               ",\n name = " + ZipFile.this.name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                               ",\n i = " + i +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                               ",\n message = " + message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   478
                        ZipEntry ze = getZipEntry(null, jzentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        freeEntry(jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   486
    private ZipEntry getZipEntry(String name, long jzentry) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   487
        ZipEntry e = new ZipEntry();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   488
        e.flag = getEntryFlag(jzentry);  // get the flag first
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   489
        if (name != null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   490
            e.name = name;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   491
        } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   492
            byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
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.name = zc.toStringUTF8(bname, bname.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.name = zc.toString(bname, bname.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
        e.time = getEntryTime(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   500
        e.crc = getEntryCrc(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   501
        e.size = getEntrySize(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   502
        e. csize = getEntryCSize(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   503
        e.method = getEntryMethod(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   504
        e.extra = getEntryBytes(jzentry, JZENTRY_EXTRA);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   505
        byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   506
        if (bcomm == null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   507
            e.comment = null;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   508
        } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   509
            if (!zc.isUTF8() && (e.flag & EFS) != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   510
                e.comment = zc.toStringUTF8(bcomm, bcomm.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   511
            } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   512
                e.comment = zc.toString(bcomm, bcomm.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   513
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   514
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   515
        return e;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   516
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   517
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private static native long getNextEntry(long jzfile, int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Returns the number of entries in the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @return the number of entries in the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Closes the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <p> Closing this ZIP file will close all of the input streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * previously returned by invocations of the {@link #getInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * getInputStream} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            closeRequested = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
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
   542
            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
   543
                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
   544
                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
   545
                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
   546
                    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
   547
            }
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   548
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            if (jzfile != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                // Close the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                long zf = this.jzfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                jzfile = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                close(zf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // Release inflaters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                synchronized (inflaters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    int size = inflaters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        Inflater inf = (Inflater)inflaters.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        inf.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
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
     * Ensures that the <code>close</code> method of this ZIP file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * called when there are no more references to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Since the time when GC would invoke this method is undetermined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * it is strongly recommended that applications invoke the <code>close</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * method as soon they have finished accessing this <code>ZipFile</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * This will prevent holding up system resources for an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * length of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @see    java.util.zip.ZipFile#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    private static native void close(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            throw new IllegalStateException("zip file closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (jzfile == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            throw new IllegalStateException("The object is not initialized.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    private void ensureOpenOrZipException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            throw new ZipException("ZipFile closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Inner class implementing the input stream used to read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * (possibly compressed) zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
   private class ZipFileInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        protected long jzentry; // address of jzentry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        private   long pos;     // current position within entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        protected long rem;     // number of remaining bytes within entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        protected long size;    // uncompressed size of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        ZipFileInputStream(long jzentry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            pos = 0;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   617
            rem = getEntryCSize(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   618
            size = getEntrySize(jzentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            this.jzentry = jzentry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            if (len > rem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                len = (int) rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                ensureOpenOrZipException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                   off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                pos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                rem -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            if (read(b, 0, 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        public long skip(long n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            if (n > rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                n = rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            rem -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
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 long size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return size;
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 void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            rem = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                if (jzentry != 0 && ZipFile.this.jzfile != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    freeEntry(ZipFile.this.jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                }
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
   683
                streams.remove(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
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
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
   689
    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
   690
                                    boolean usemmap) throws IOException;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   691
    private static native int getTotal(long jzfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    private static native int read(long jzfile, long jzentry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                                   long pos, byte[] b, int off, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   695
    // access to the native zentry object
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   696
    private static native long getEntryTime(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   697
    private static native long getEntryCrc(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   698
    private static native long getEntryCSize(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   699
    private static native long getEntrySize(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   700
    private static native int getEntryMethod(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   701
    private static native int getEntryFlag(long jzentry);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   702
    private static native byte[] getCommentBytes(long jzfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   704
    private static final int JZENTRY_NAME = 0;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   705
    private static final int JZENTRY_EXTRA = 1;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   706
    private static final int JZENTRY_COMMENT = 2;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   707
    private static native byte[] getEntryBytes(long jzentry, int type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    private static native String getZipMessage(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
}