1 /*
   2  * Copyright (c) 1995, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.util.Date;
  29 import sun.misc.BootClassLoaderHook;
  30 
  31 /**
  32  * This class is used to represent a ZIP file entry.
  33  *
  34  * @author      David Connelly
  35  */
  36 public
  37 class ZipEntry implements ZipConstants, Cloneable {
  38     String name;        // entry name
  39     long time = -1;     // modification time (in DOS time)
  40     long crc = -1;      // crc-32 of entry data
  41     long size = -1;     // uncompressed size of entry data
  42     long csize = -1;    // compressed size of entry data
  43     int method = -1;    // compression method
  44     int flag = 0;       // general purpose flag
  45     byte[] extra;       // optional extra field data for entry
  46     String comment;     // optional comment string for entry
  47 
  48     /**
  49      * Compression method for uncompressed entries.
  50      */
  51     public static final int STORED = 0;
  52 
  53     /**
  54      * Compression method for compressed (deflated) entries.
  55      */
  56     public static final int DEFLATED = 8;
  57 
  58     /**
  59      * Creates a new zip entry with the specified name.
  60      *
  61      * @param name the entry name
  62      * @exception NullPointerException if the entry name is null
  63      * @exception IllegalArgumentException if the entry name is longer than
  64      *            0xFFFF bytes
  65      */
  66     public ZipEntry(String name) {
  67         if (name == null) {
  68             throw new NullPointerException();
  69         }
  70         if (name.length() > 0xFFFF) {
  71             throw new IllegalArgumentException("entry name too long");
  72         }
  73         this.name = name;
  74     }
  75 
  76     /**
  77      * Creates a new zip entry with fields taken from the specified
  78      * zip entry.
  79      * @param e a zip Entry object
  80      */
  81     public ZipEntry(ZipEntry e) {
  82         name = e.name;
  83         time = e.time;
  84         crc = e.crc;
  85         size = e.size;
  86         csize = e.csize;
  87         method = e.method;
  88         flag = e.flag;
  89         extra = e.extra;
  90         comment = e.comment;
  91     }
  92 
  93     /*
  94      * Creates a new un-initialized zip entry
  95      */
  96     ZipEntry() {}
  97 
  98     /**
  99      * Returns the name of the entry.
 100      * @return the name of the entry
 101      */
 102     public String getName() {
 103         return name;
 104     }
 105 
 106     /**
 107      * Sets the modification time of the entry.
 108      * @param time the entry modification time in number of milliseconds
 109      *             since the epoch
 110      * @see #getTime()
 111      */
 112     public void setTime(long time) {
 113         this.time = javaToDosTime(time);
 114     }
 115 
 116     /**
 117      * Returns the modification time of the entry, or -1 if not specified.
 118      * @return the modification time of the entry, or -1 if not specified
 119      * @see #setTime(long)
 120      */
 121     public long getTime() {
 122         return time != -1 ? dosToJavaTime(time) : -1;
 123     }
 124 
 125     /**
 126      * Sets the uncompressed size of the entry data.
 127      * @param size the uncompressed size in bytes
 128      * @exception IllegalArgumentException if the specified size is less
 129      *            than 0, is greater than 0xFFFFFFFF when
 130      *            <a href="package-summary.html#zip64">ZIP64 format</a> is not supported,
 131      *            or is less than 0 when ZIP64 is supported
 132      * @see #getSize()
 133      */
 134     public void setSize(long size) {
 135         if (size < 0) {
 136             throw new IllegalArgumentException("invalid entry size");
 137         }
 138         this.size = size;
 139     }
 140 
 141     /**
 142      * Returns the uncompressed size of the entry data, or -1 if not known.
 143      * @return the uncompressed size of the entry data, or -1 if not known
 144      * @see #setSize(long)
 145      */
 146     public long getSize() {
 147         return size;
 148     }
 149 
 150     /**
 151      * Returns the size of the compressed entry data, or -1 if not known.
 152      * In the case of a stored entry, the compressed size will be the same
 153      * as the uncompressed size of the entry.
 154      * @return the size of the compressed entry data, or -1 if not known
 155      * @see #setCompressedSize(long)
 156      */
 157     public long getCompressedSize() {
 158         return csize;
 159     }
 160 
 161     /**
 162      * Sets the size of the compressed entry data.
 163      * @param csize the compressed size to set to
 164      * @see #getCompressedSize()
 165      */
 166     public void setCompressedSize(long csize) {
 167         this.csize = csize;
 168     }
 169 
 170     /**
 171      * Sets the CRC-32 checksum of the uncompressed entry data.
 172      * @param crc the CRC-32 value
 173      * @exception IllegalArgumentException if the specified CRC-32 value is
 174      *            less than 0 or greater than 0xFFFFFFFF
 175      * @see #getCrc()
 176      */
 177     public void setCrc(long crc) {
 178         if (crc < 0 || crc > 0xFFFFFFFFL) {
 179             throw new IllegalArgumentException("invalid entry crc-32");
 180         }
 181         this.crc = crc;
 182     }
 183 
 184     /**
 185      * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
 186      * not known.
 187      * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
 188      * not known
 189      * @see #setCrc(long)
 190      */
 191     public long getCrc() {
 192         return crc;
 193     }
 194 
 195     /**
 196      * Sets the compression method for the entry.
 197      * @param method the compression method, either STORED or DEFLATED
 198      * @exception IllegalArgumentException if the specified compression
 199      *            method is invalid
 200      * @see #getMethod()
 201      */
 202     public void setMethod(int method) {
 203         if (method != STORED && method != DEFLATED) {
 204             throw new IllegalArgumentException("invalid compression method");
 205         }
 206         this.method = method;
 207     }
 208 
 209     /**
 210      * Returns the compression method of the entry, or -1 if not specified.
 211      * @return the compression method of the entry, or -1 if not specified
 212      * @see #setMethod(int)
 213      */
 214     public int getMethod() {
 215         return method;
 216     }
 217 
 218     /**
 219      * Sets the optional extra field data for the entry.
 220      * @param extra the extra field data bytes
 221      * @exception IllegalArgumentException if the length of the specified
 222      *            extra field data is greater than 0xFFFF bytes
 223      * @see #getExtra()
 224      */
 225     public void setExtra(byte[] extra) {
 226         if (extra != null && extra.length > 0xFFFF) {
 227             throw new IllegalArgumentException("invalid extra field length");
 228         }
 229         this.extra = extra;
 230     }
 231 
 232     /**
 233      * Returns the extra field data for the entry, or null if none.
 234      * @return the extra field data for the entry, or null if none
 235      * @see #setExtra(byte[])
 236      */
 237     public byte[] getExtra() {
 238         return extra;
 239     }
 240 
 241     /**
 242      * Sets the optional comment string for the entry.
 243      *
 244      * <p>ZIP entry comments have maximum length of 0xffff. If the length of the
 245      * specified comment string is greater than 0xFFFF bytes after encoding, only
 246      * the first 0xFFFF bytes are output to the ZIP file entry.
 247      *
 248      * @param comment the comment string
 249      *
 250      * @see #getComment()
 251      */
 252     public void setComment(String comment) {
 253         this.comment = comment;
 254     }
 255 
 256     /**
 257      * Returns the comment string for the entry, or null if none.
 258      * @return the comment string for the entry, or null if none
 259      * @see #setComment(String)
 260      */
 261     public String getComment() {
 262         return comment;
 263     }
 264 
 265     /**
 266      * Returns true if this is a directory entry. A directory entry is
 267      * defined to be one whose name ends with a '/'.
 268      * @return true if this is a directory entry
 269      */
 270     public boolean isDirectory() {
 271         return name.endsWith("/");
 272     }
 273 
 274     /**
 275      * Returns a string representation of the ZIP entry.
 276      */
 277     public String toString() {
 278         return getName();
 279     }
 280 
 281     /*
 282      * Converts DOS time to Java time (number of milliseconds since epoch).
 283      */
 284     private static long dosToJavaTime(long dtime) {
 285         Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
 286                           (int)(((dtime >> 21) & 0x0f) - 1),
 287                           (int)((dtime >> 16) & 0x1f),
 288                           (int)((dtime >> 11) & 0x1f),
 289                           (int)((dtime >> 5) & 0x3f),
 290                           (int)((dtime << 1) & 0x3e));
 291         return d.getTime();
 292     }
 293 
 294     /*
 295      * Converts Java time to DOS time.
 296      */
 297     private static long javaToDosTime(long time) {
 298         Date d = new Date(time);
 299         int year = d.getYear() + 1900;
 300         if (year < 1980) {
 301             return (1 << 21) | (1 << 16);
 302         }
 303         return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
 304                d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
 305                d.getSeconds() >> 1;
 306     }
 307 
 308     /**
 309      * Returns the hash code value for this entry.
 310      */
 311     public int hashCode() {
 312         return name.hashCode();
 313     }
 314 
 315     /**
 316      * Returns a copy of this entry.
 317      */
 318     public Object clone() {
 319         try {
 320             ZipEntry e = (ZipEntry)super.clone();
 321             e.extra = (extra == null) ? null : extra.clone();
 322             return e;
 323         } catch (CloneNotSupportedException e) {
 324             // This should never happen, since we are Cloneable
 325             throw new InternalError();
 326         }
 327     }
 328 }