jdk/src/share/classes/java/util/zip/ZipEntry.java
author sherman
Fri, 11 Sep 2009 16:36:22 -0700
changeset 3844 74c6b6e2db6c
parent 3111 fefdeafb7ab9
child 3958 b8acd5ee4f4f
permissions -rw-r--r--
6881337: ZipEntry.setComment() was accidentally changed back to old spec/impl in jdk7-b64 Summary: restored the correct spec and implementation of setComment Reviewed-by: 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
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    43
    int flag = 0;       // general purpose flag
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    byte[] extra;       // optional extra field data for entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    String comment;     // optional comment string for entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Compression method for uncompressed entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static final int STORED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Compression method for compressed (deflated) entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final int DEFLATED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Creates a new zip entry with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param name the entry name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @exception NullPointerException if the entry name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @exception IllegalArgumentException if the entry name is longer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *            0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public ZipEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (name.length() > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new IllegalArgumentException("entry name too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Creates a new zip entry with fields taken from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * zip entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param e a zip Entry object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public ZipEntry(ZipEntry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        name = e.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        time = e.time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        crc = e.crc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        size = e.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        csize = e.csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        method = e.method;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    87
        flag = e.flag;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        extra = e.extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        comment = e.comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /*
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    93
     * Creates a new un-initialized zip entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    95
    ZipEntry() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Returns the name of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return the name of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Sets the modification time of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param time the entry modification time in number of milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *             since the epoch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @see #getTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void setTime(long time) {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   112
        // fix for bug 6625963: we bypass time calculations while Kernel is
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   113
        // downloading bundles, since they aren't necessary and would cause
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   114
        // the Kernel core to depend upon the (very large) time zone data
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   115
        if (sun.misc.VM.isBootedKernelVM() &&
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   116
            sun.jkernel.DownloadManager.isCurrentThreadDownloading()) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   117
            this.time = sun.jkernel.DownloadManager.KERNEL_STATIC_MODTIME;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   118
        } else {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   119
            this.time = javaToDosTime(time);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2592
diff changeset
   120
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Returns the modification time of the entry, or -1 if not specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return the modification time of the entry, or -1 if not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see #setTime(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return time != -1 ? dosToJavaTime(time) : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Sets the uncompressed size of the entry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param size the uncompressed size in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception IllegalArgumentException if the specified size is less
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   136
     *            than 0, is greater than 0xFFFFFFFF when
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   137
     *            <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
   138
     *            or is less than 0 when ZIP64 is supported
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see #getSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void setSize(long size) {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   142
        if (size < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new IllegalArgumentException("invalid entry size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Returns the uncompressed size of the entry data, or -1 if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the uncompressed size of the entry data, or -1 if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see #setSize(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public long getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the size of the compressed entry data, or -1 if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * In the case of a stored entry, the compressed size will be the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * as the uncompressed size of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the size of the compressed entry data, or -1 if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see #setCompressedSize(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public long getCompressedSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return csize;
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
     * Sets the size of the compressed entry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param csize the compressed size to set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see #getCompressedSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void setCompressedSize(long csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.csize = csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Sets the CRC-32 checksum of the uncompressed entry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param crc the CRC-32 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception IllegalArgumentException if the specified CRC-32 value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *            less than 0 or greater than 0xFFFFFFFF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see #getCrc()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void setCrc(long crc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (crc < 0 || crc > 0xFFFFFFFFL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new IllegalArgumentException("invalid entry crc-32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.crc = crc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see #setCrc(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public long getCrc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return 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
     * Sets the compression method for the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param method the compression method, either STORED or DEFLATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @exception IllegalArgumentException if the specified compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *            method is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @see #getMethod()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void setMethod(int method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (method != STORED && method != DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throw new IllegalArgumentException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this.method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Returns the compression method of the entry, or -1 if not specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @return the compression method of the entry, or -1 if not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see #setMethod(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public int getMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Sets the optional extra field data for the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param extra the extra field data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @exception IllegalArgumentException if the length of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *            extra field data is greater than 0xFFFF bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @see #getExtra()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public void setExtra(byte[] extra) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (extra != null && extra.length > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new IllegalArgumentException("invalid extra field length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        this.extra = extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Returns the extra field data for the entry, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return the extra field data for the entry, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see #setExtra(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public byte[] getExtra() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Sets the optional comment string for the entry.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   250
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   251
     * <p>ZIP entry comments have maximum length of 0xffff. If the length of the
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   252
     * specified comment string is greater than 0xFFFF bytes after encoding, only
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   253
     * the first 0xFFFF bytes are output to the ZIP file entry.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   254
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param comment the comment string
3844
74c6b6e2db6c 6881337: ZipEntry.setComment() was accidentally changed back to old spec/impl in jdk7-b64
sherman
parents: 3111
diff changeset
   256
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see #getComment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public void setComment(String comment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        this.comment = comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Returns the comment string for the entry, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return the comment string for the entry, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see #setComment(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public String getComment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns true if this is a directory entry. A directory entry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * defined to be one whose name ends with a '/'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return true if this is a directory entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public boolean isDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return name.endsWith("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns a string representation of the ZIP entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Converts DOS time to Java time (number of milliseconds since epoch).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private static long dosToJavaTime(long dtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                          (int)(((dtime >> 21) & 0x0f) - 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                          (int)((dtime >> 16) & 0x1f),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                          (int)((dtime >> 11) & 0x1f),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                          (int)((dtime >> 5) & 0x3f),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                          (int)((dtime << 1) & 0x3e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return d.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Converts Java time to DOS time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    private static long javaToDosTime(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        Date d = new Date(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int year = d.getYear() + 1900;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (year < 1980) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return (1 << 21) | (1 << 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
               d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
               d.getSeconds() >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns the hash code value for this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return name.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Returns a copy of this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            ZipEntry e = (ZipEntry)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            e.extra = (extra == null) ? null : extra.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // This should never happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}