jdk/src/share/classes/java/util/zip/ZipEntry.java
author sherman
Thu, 02 Apr 2009 15:35:46 -0700
changeset 2438 21c111b51aa8
parent 2 90ce3da70b43
child 2592 ef26f663a2ba
permissions -rw-r--r--
4681995: Add support for large (> 4GB) zip/jar files Summary: The ZIP64 format support is added for > 4GB jar/zip files Reviewed-by: alanb, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
     2
 * Copyright 1995-2009 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class is used to represent a ZIP file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class ZipEntry implements ZipConstants, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    String name;        // entry name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    long time = -1;     // modification time (in DOS time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    long crc = -1;      // crc-32 of entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    long size = -1;     // uncompressed size of entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    long csize = -1;    // compressed size of entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    int method = -1;    // compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    byte[] extra;       // optional extra field data for entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    String comment;     // optional comment string for entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Compression method for uncompressed entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final int STORED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Compression method for compressed (deflated) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final int DEFLATED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        /* Zip library is loaded from System.initializeSystemClass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a new zip entry with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param name the entry name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @exception NullPointerException if the entry name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @exception IllegalArgumentException if the entry name is longer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *            0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public ZipEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (name.length() > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new IllegalArgumentException("entry name too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a new zip entry with fields taken from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * zip entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param e a zip Entry object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public ZipEntry(ZipEntry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        name = e.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        time = e.time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        crc = e.crc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        size = e.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        csize = e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        method = e.method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        extra = e.extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        comment = e.comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Creates a new zip entry for the given name with fields initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * from the specified jzentry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    ZipEntry(String name, long jzentry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        initFields(jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private native void initFields(long jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Creates a new zip entry with fields initialized from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * jzentry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    ZipEntry(long jzentry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        initFields(jzentry);
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
     * Returns the name of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return the name of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Sets the modification time of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param time the entry modification time in number of milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *             since the epoch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see #getTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void setTime(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.time = javaToDosTime(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Returns the modification time of the entry, or -1 if not specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return the modification time of the entry, or -1 if not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see #setTime(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return time != -1 ? dosToJavaTime(time) : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Sets the uncompressed size of the entry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param size the uncompressed size in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @exception IllegalArgumentException if the specified size is less
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   147
     *            than 0, is greater than 0xFFFFFFFF when
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   148
     *            <a href="package-summary.html#zip64">ZIP64 format</a> is not supported,
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   149
     *            or is less than 0 when ZIP64 is supported
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see #getSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void setSize(long size) {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   153
        if (size < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException("invalid entry size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns the uncompressed size of the entry data, or -1 if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the uncompressed size of the entry data, or -1 if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see #setSize(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public long getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns the size of the compressed entry data, or -1 if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * In the case of a stored entry, the compressed size will be the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * as the uncompressed size of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return the size of the compressed entry data, or -1 if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see #setCompressedSize(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public long getCompressedSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Sets the size of the compressed entry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param csize the compressed size to set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see #getCompressedSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void setCompressedSize(long csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.csize = csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Sets the CRC-32 checksum of the uncompressed entry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param crc the CRC-32 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @exception IllegalArgumentException if the specified CRC-32 value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *            less than 0 or greater than 0xFFFFFFFF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see #getCrc()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public void setCrc(long crc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (crc < 0 || crc > 0xFFFFFFFFL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new IllegalArgumentException("invalid entry crc-32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this.crc = crc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @see #setCrc(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public long getCrc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return crc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Sets the compression method for the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param method the compression method, either STORED or DEFLATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception IllegalArgumentException if the specified compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *            method is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see #getMethod()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public void setMethod(int method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (method != STORED && method != DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new IllegalArgumentException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        this.method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Returns the compression method of the entry, or -1 if not specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return the compression method of the entry, or -1 if not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @see #setMethod(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public int getMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Sets the optional extra field data for the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param extra the extra field data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @exception IllegalArgumentException if the length of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *            extra field data is greater than 0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see #getExtra()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public void setExtra(byte[] extra) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (extra != null && extra.length > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new IllegalArgumentException("invalid extra field length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        this.extra = extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Returns the extra field data for the entry, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return the extra field data for the entry, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see #setExtra(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public byte[] getExtra() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Sets the optional comment string for the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param comment the comment string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @exception IllegalArgumentException if the length of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *            comment string is greater than 0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see #getComment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public void setComment(String comment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (comment != null && comment.length() > 0xffff/3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    && ZipOutputStream.getUTF8Length(comment) > 0xffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new IllegalArgumentException("invalid entry comment length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        this.comment = comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Returns the comment string for the entry, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return the comment string for the entry, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see #setComment(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public String getComment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Returns true if this is a directory entry. A directory entry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * defined to be one whose name ends with a '/'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return true if this is a directory entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public boolean isDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return name.endsWith("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Returns a string representation of the ZIP entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Converts DOS time to Java time (number of milliseconds since epoch).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private static long dosToJavaTime(long dtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                          (int)(((dtime >> 21) & 0x0f) - 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                          (int)((dtime >> 16) & 0x1f),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                          (int)((dtime >> 11) & 0x1f),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                          (int)((dtime >> 5) & 0x3f),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                          (int)((dtime << 1) & 0x3e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return d.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Converts Java time to DOS time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static long javaToDosTime(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Date d = new Date(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int year = d.getYear() + 1900;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (year < 1980) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return (1 << 21) | (1 << 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
               d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
               d.getSeconds() >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Returns the hash code value for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return name.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns a copy of this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            ZipEntry e = (ZipEntry)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            e.extra = (extra == null) ? null : extra.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // This should never happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}