rt/emul/zip/src/main/java/java/util/zip/ZipEntry.java
branchclosure
changeset 1549 3f4c143ff8f0
parent 772 d382dacfd73f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/zip/src/main/java/java/util/zip/ZipEntry.java	Wed May 07 16:47:24 2014 +0200
     1.3 @@ -0,0 +1,331 @@
     1.4 +/*
     1.5 + * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util.zip;
    1.30 +
    1.31 +/**
    1.32 + * This class is used to represent a ZIP file entry.
    1.33 + *
    1.34 + * @author      David Connelly
    1.35 + */
    1.36 +public
    1.37 +class ZipEntry implements ZipConstants, Cloneable {
    1.38 +    String name;        // entry name
    1.39 +    long time = -1;     // modification time (in DOS time)
    1.40 +    long crc = -1;      // crc-32 of entry data
    1.41 +    long size = -1;     // uncompressed size of entry data
    1.42 +    long csize = -1;    // compressed size of entry data
    1.43 +    int method = -1;    // compression method
    1.44 +    int flag = 0;       // general purpose flag
    1.45 +    byte[] extra;       // optional extra field data for entry
    1.46 +    String comment;     // optional comment string for entry
    1.47 +
    1.48 +    /**
    1.49 +     * Compression method for uncompressed entries.
    1.50 +     */
    1.51 +    public static final int STORED = 0;
    1.52 +
    1.53 +    /**
    1.54 +     * Compression method for compressed (deflated) entries.
    1.55 +     */
    1.56 +    public static final int DEFLATED = 8;
    1.57 +
    1.58 +    /**
    1.59 +     * Creates a new zip entry with the specified name.
    1.60 +     *
    1.61 +     * @param name the entry name
    1.62 +     * @exception NullPointerException if the entry name is null
    1.63 +     * @exception IllegalArgumentException if the entry name is longer than
    1.64 +     *            0xFFFF bytes
    1.65 +     */
    1.66 +    public ZipEntry(String name) {
    1.67 +        if (name == null) {
    1.68 +            throw new NullPointerException();
    1.69 +        }
    1.70 +        if (name.length() > 0xFFFF) {
    1.71 +            throw new IllegalArgumentException("entry name too long");
    1.72 +        }
    1.73 +        this.name = name;
    1.74 +    }
    1.75 +
    1.76 +    /**
    1.77 +     * Creates a new zip entry with fields taken from the specified
    1.78 +     * zip entry.
    1.79 +     * @param e a zip Entry object
    1.80 +     */
    1.81 +    public ZipEntry(ZipEntry e) {
    1.82 +        name = e.name;
    1.83 +        time = e.time;
    1.84 +        crc = e.crc;
    1.85 +        size = e.size;
    1.86 +        csize = e.csize;
    1.87 +        method = e.method;
    1.88 +        flag = e.flag;
    1.89 +        extra = e.extra;
    1.90 +        comment = e.comment;
    1.91 +    }
    1.92 +
    1.93 +    /*
    1.94 +     * Creates a new un-initialized zip entry
    1.95 +     */
    1.96 +    ZipEntry() {}
    1.97 +
    1.98 +    /**
    1.99 +     * Returns the name of the entry.
   1.100 +     * @return the name of the entry
   1.101 +     */
   1.102 +    public String getName() {
   1.103 +        return name;
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * Sets the modification time of the entry.
   1.108 +     * @param time the entry modification time in number of milliseconds
   1.109 +     *             since the epoch
   1.110 +     * @see #getTime()
   1.111 +     */
   1.112 +    public void setTime(long time) {
   1.113 +        this.time = javaToDosTime(time);
   1.114 +    }
   1.115 +
   1.116 +    /**
   1.117 +     * Returns the modification time of the entry, or -1 if not specified.
   1.118 +     * @return the modification time of the entry, or -1 if not specified
   1.119 +     * @see #setTime(long)
   1.120 +     */
   1.121 +    public long getTime() {
   1.122 +        return time != -1 ? dosToJavaTime(time) : -1;
   1.123 +    }
   1.124 +
   1.125 +    /**
   1.126 +     * Sets the uncompressed size of the entry data.
   1.127 +     * @param size the uncompressed size in bytes
   1.128 +     * @exception IllegalArgumentException if the specified size is less
   1.129 +     *            than 0, is greater than 0xFFFFFFFF when
   1.130 +     *            <a href="package-summary.html#zip64">ZIP64 format</a> is not supported,
   1.131 +     *            or is less than 0 when ZIP64 is supported
   1.132 +     * @see #getSize()
   1.133 +     */
   1.134 +    public void setSize(long size) {
   1.135 +        if (size < 0) {
   1.136 +            throw new IllegalArgumentException("invalid entry size");
   1.137 +        }
   1.138 +        this.size = size;
   1.139 +    }
   1.140 +
   1.141 +    /**
   1.142 +     * Returns the uncompressed size of the entry data, or -1 if not known.
   1.143 +     * @return the uncompressed size of the entry data, or -1 if not known
   1.144 +     * @see #setSize(long)
   1.145 +     */
   1.146 +    public long getSize() {
   1.147 +        return size;
   1.148 +    }
   1.149 +
   1.150 +    /**
   1.151 +     * Returns the size of the compressed entry data, or -1 if not known.
   1.152 +     * In the case of a stored entry, the compressed size will be the same
   1.153 +     * as the uncompressed size of the entry.
   1.154 +     * @return the size of the compressed entry data, or -1 if not known
   1.155 +     * @see #setCompressedSize(long)
   1.156 +     */
   1.157 +    public long getCompressedSize() {
   1.158 +        return csize;
   1.159 +    }
   1.160 +
   1.161 +    /**
   1.162 +     * Sets the size of the compressed entry data.
   1.163 +     * @param csize the compressed size to set to
   1.164 +     * @see #getCompressedSize()
   1.165 +     */
   1.166 +    public void setCompressedSize(long csize) {
   1.167 +        this.csize = csize;
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Sets the CRC-32 checksum of the uncompressed entry data.
   1.172 +     * @param crc the CRC-32 value
   1.173 +     * @exception IllegalArgumentException if the specified CRC-32 value is
   1.174 +     *            less than 0 or greater than 0xFFFFFFFF
   1.175 +     * @see #getCrc()
   1.176 +     */
   1.177 +    public void setCrc(long crc) {
   1.178 +        if (crc < 0 || crc > 0xFFFFFFFFL) {
   1.179 +            throw new IllegalArgumentException("invalid entry crc-32");
   1.180 +        }
   1.181 +        this.crc = crc;
   1.182 +    }
   1.183 +
   1.184 +    /**
   1.185 +     * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
   1.186 +     * not known.
   1.187 +     * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
   1.188 +     * not known
   1.189 +     * @see #setCrc(long)
   1.190 +     */
   1.191 +    public long getCrc() {
   1.192 +        return crc;
   1.193 +    }
   1.194 +
   1.195 +    /**
   1.196 +     * Sets the compression method for the entry.
   1.197 +     * @param method the compression method, either STORED or DEFLATED
   1.198 +     * @exception IllegalArgumentException if the specified compression
   1.199 +     *            method is invalid
   1.200 +     * @see #getMethod()
   1.201 +     */
   1.202 +    public void setMethod(int method) {
   1.203 +        if (method != STORED && method != DEFLATED) {
   1.204 +            throw new IllegalArgumentException("invalid compression method");
   1.205 +        }
   1.206 +        this.method = method;
   1.207 +    }
   1.208 +
   1.209 +    /**
   1.210 +     * Returns the compression method of the entry, or -1 if not specified.
   1.211 +     * @return the compression method of the entry, or -1 if not specified
   1.212 +     * @see #setMethod(int)
   1.213 +     */
   1.214 +    public int getMethod() {
   1.215 +        return method;
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Sets the optional extra field data for the entry.
   1.220 +     * @param extra the extra field data bytes
   1.221 +     * @exception IllegalArgumentException if the length of the specified
   1.222 +     *            extra field data is greater than 0xFFFF bytes
   1.223 +     * @see #getExtra()
   1.224 +     */
   1.225 +    public void setExtra(byte[] extra) {
   1.226 +        if (extra != null && extra.length > 0xFFFF) {
   1.227 +            throw new IllegalArgumentException("invalid extra field length");
   1.228 +        }
   1.229 +        this.extra = extra;
   1.230 +    }
   1.231 +
   1.232 +    /**
   1.233 +     * Returns the extra field data for the entry, or null if none.
   1.234 +     * @return the extra field data for the entry, or null if none
   1.235 +     * @see #setExtra(byte[])
   1.236 +     */
   1.237 +    public byte[] getExtra() {
   1.238 +        return extra;
   1.239 +    }
   1.240 +
   1.241 +    /**
   1.242 +     * Sets the optional comment string for the entry.
   1.243 +     *
   1.244 +     * <p>ZIP entry comments have maximum length of 0xffff. If the length of the
   1.245 +     * specified comment string is greater than 0xFFFF bytes after encoding, only
   1.246 +     * the first 0xFFFF bytes are output to the ZIP file entry.
   1.247 +     *
   1.248 +     * @param comment the comment string
   1.249 +     *
   1.250 +     * @see #getComment()
   1.251 +     */
   1.252 +    public void setComment(String comment) {
   1.253 +        this.comment = comment;
   1.254 +    }
   1.255 +
   1.256 +    /**
   1.257 +     * Returns the comment string for the entry, or null if none.
   1.258 +     * @return the comment string for the entry, or null if none
   1.259 +     * @see #setComment(String)
   1.260 +     */
   1.261 +    public String getComment() {
   1.262 +        return comment;
   1.263 +    }
   1.264 +
   1.265 +    /**
   1.266 +     * Returns true if this is a directory entry. A directory entry is
   1.267 +     * defined to be one whose name ends with a '/'.
   1.268 +     * @return true if this is a directory entry
   1.269 +     */
   1.270 +    public boolean isDirectory() {
   1.271 +        return name.endsWith("/");
   1.272 +    }
   1.273 +
   1.274 +    /**
   1.275 +     * Returns a string representation of the ZIP entry.
   1.276 +     */
   1.277 +    public String toString() {
   1.278 +        return getName();
   1.279 +    }
   1.280 +
   1.281 +    /*
   1.282 +     * Converts DOS time to Java time (number of milliseconds since epoch).
   1.283 +     */
   1.284 +    private static long dosToJavaTime(long dtime) {
   1.285 +        return dtime;
   1.286 +        /* XXX:
   1.287 +        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
   1.288 +                          (int)(((dtime >> 21) & 0x0f) - 1),
   1.289 +                          (int)((dtime >> 16) & 0x1f),
   1.290 +                          (int)((dtime >> 11) & 0x1f),
   1.291 +                          (int)((dtime >> 5) & 0x3f),
   1.292 +                          (int)((dtime << 1) & 0x3e));
   1.293 +        return d.getTime();
   1.294 +        */
   1.295 +    }
   1.296 +
   1.297 +    /*
   1.298 +     * Converts Java time to DOS time.
   1.299 +     */
   1.300 +    private static long javaToDosTime(long time) {
   1.301 +        return time;
   1.302 +        /* XXX:
   1.303 +        Date d = new Date(time);
   1.304 +        int year = d.getYear() + 1900;
   1.305 +        if (year < 1980) {
   1.306 +            return (1 << 21) | (1 << 16);
   1.307 +        }
   1.308 +        return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
   1.309 +               d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
   1.310 +               d.getSeconds() >> 1;
   1.311 +        */
   1.312 +    }
   1.313 +
   1.314 +    /**
   1.315 +     * Returns the hash code value for this entry.
   1.316 +     */
   1.317 +    public int hashCode() {
   1.318 +        return name.hashCode();
   1.319 +    }
   1.320 +
   1.321 +    /**
   1.322 +     * Returns a copy of this entry.
   1.323 +     */
   1.324 +    public Object clone() {
   1.325 +        try {
   1.326 +            ZipEntry e = (ZipEntry)super.clone();
   1.327 +            e.extra = (extra == null) ? null : extra.clone();
   1.328 +            return e;
   1.329 +        } catch (CloneNotSupportedException e) {
   1.330 +            // This should never happen, since we are Cloneable
   1.331 +            throw new IllegalStateException();
   1.332 +        }
   1.333 +    }
   1.334 +}