1 /*
   2  * Copyright (c) 1995, 2011, 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 /**
  29  * This class is used to represent a ZIP file entry.
  30  *
  31  * @author      David Connelly
  32  */
  33 public
  34 class ZipEntry implements ZipConstants, Cloneable {
  35     String name;        // entry name
  36     long mtime = -1;    // last modification time
  37     long crc = -1;      // crc-32 of entry data
  38     long size = -1;     // uncompressed size of entry data
  39     long csize = -1;    // compressed size of entry data
  40     int method = -1;    // compression method
  41     int flag = 0;       // general purpose flag
  42     byte[] extra;       // optional extra field data for entry
  43     String comment;     // optional comment string for entry
  44 
  45     /**
  46      * Compression method for uncompressed entries.
  47      */
  48     public static final int STORED = 0;
  49 
  50     /**
  51      * Compression method for compressed (deflated) entries.
  52      */
  53     public static final int DEFLATED = 8;
  54 
  55     /**
  56      * Creates a new zip entry with the specified name.
  57      *
  58      * @param name the entry name
  59      * @exception NullPointerException if the entry name is null
  60      * @exception IllegalArgumentException if the entry name is longer than
  61      *            0xFFFF bytes
  62      */
  63     public ZipEntry(String name) {
  64         if (name == null) {
  65             throw new NullPointerException();
  66         }
  67         if (name.length() > 0xFFFF) {
  68             throw new IllegalArgumentException("entry name too long");
  69         }
  70         this.name = name;
  71     }
  72 
  73     /**
  74      * Creates a new zip entry with fields taken from the specified
  75      * zip entry.
  76      * @param e a zip Entry object
  77      */
  78     public ZipEntry(ZipEntry e) {
  79         name = e.name;
  80         mtime = e.mtime;
  81         crc = e.crc;
  82         size = e.size;
  83         csize = e.csize;
  84         method = e.method;
  85         flag = e.flag;
  86         extra = e.extra;
  87         comment = e.comment;
  88     }
  89 
  90     /**
  91      * Creates a new un-initialized zip entry
  92      */
  93     ZipEntry() {}
  94 
  95     /**
  96      * Returns the name of the entry.
  97      * @return the name of the entry
  98      */
  99     public String getName() {
 100         return name;
 101     }
 102 
 103     /**
 104      * Sets the last modification time of the entry.
 105      *
 106      * @param time the last modification time of the entry in milliseconds since the epoch
 107      * @see #getTime()
 108      */
 109     public void setTime(long time) {
 110         this.mtime = time;
 111     }
 112 
 113     /**
 114      * Returns the last modification time of the entry.
 115      * <p> The last modificatin time may come from zip entry's extensible
 116      * data field {@code NTFS} or {@code Info-ZIP Extended Timestamp}, if
 117      * the entry is read from {@link ZipInputStream} or {@link ZipFile}.
 118      *
 119      * @return the last modification time of the entry, or -1 if not specified
 120      * @see #setTime(long)
 121      */
 122     public long getTime() {
 123         return mtime;
 124     }
 125 
 126     /**
 127      * Sets the uncompressed size of the entry data.
 128      * @param size the uncompressed size in bytes
 129      * @exception IllegalArgumentException if the specified size is less
 130      *            than 0, is greater than 0xFFFFFFFF when
 131      *            <a href="package-summary.html#zip64">ZIP64 format</a> is not supported,
 132      *            or is less than 0 when ZIP64 is supported
 133      * @see #getSize()
 134      */
 135     public void setSize(long size) {
 136         if (size < 0) {
 137             throw new IllegalArgumentException("invalid entry size");
 138         }
 139         this.size = size;
 140     }
 141 
 142     /**
 143      * Returns the uncompressed size of the entry data, or -1 if not known.
 144      * @return the uncompressed size of the entry data, or -1 if not known
 145      * @see #setSize(long)
 146      */
 147     public long getSize() {
 148         return size;
 149     }
 150 
 151     /**
 152      * Returns the size of the compressed entry data, or -1 if not known.
 153      * In the case of a stored entry, the compressed size will be the same
 154      * as the uncompressed size of the entry.
 155      * @return the size of the compressed entry data, or -1 if not known
 156      * @see #setCompressedSize(long)
 157      */
 158     public long getCompressedSize() {
 159         return csize;
 160     }
 161 
 162     /**
 163      * Sets the size of the compressed entry data.
 164      * @param csize the compressed size to set to
 165      * @see #getCompressedSize()
 166      */
 167     public void setCompressedSize(long csize) {
 168         this.csize = csize;
 169     }
 170 
 171     /**
 172      * Sets the CRC-32 checksum of the uncompressed entry data.
 173      * @param crc the CRC-32 value
 174      * @exception IllegalArgumentException if the specified CRC-32 value is
 175      *            less than 0 or greater than 0xFFFFFFFF
 176      * @see #getCrc()
 177      */
 178     public void setCrc(long crc) {
 179         if (crc < 0 || crc > 0xFFFFFFFFL) {
 180             throw new IllegalArgumentException("invalid entry crc-32");
 181         }
 182         this.crc = crc;
 183     }
 184 
 185     /**
 186      * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
 187      * not known.
 188      * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
 189      * not known
 190      * @see #setCrc(long)
 191      */
 192     public long getCrc() {
 193         return crc;
 194     }
 195 
 196     /**
 197      * Sets the compression method for the entry.
 198      * @param method the compression method, either STORED or DEFLATED
 199      * @exception IllegalArgumentException if the specified compression
 200      *            method is invalid
 201      * @see #getMethod()
 202      */
 203     public void setMethod(int method) {
 204         if (method != STORED && method != DEFLATED) {
 205             throw new IllegalArgumentException("invalid compression method");
 206         }
 207         this.method = method;
 208     }
 209 
 210     /**
 211      * Returns the compression method of the entry, or -1 if not specified.
 212      * @return the compression method of the entry, or -1 if not specified
 213      * @see #setMethod(int)
 214      */
 215     public int getMethod() {
 216         return method;
 217     }
 218 
 219     /**
 220      * Sets the optional extra field data for the entry.
 221      * @param extra the extra field data bytes
 222      * @exception IllegalArgumentException if the length of the specified
 223      *            extra field data is greater than 0xFFFF bytes
 224      * @see #getExtra()
 225      */
 226     public void setExtra(byte[] extra) {
 227         if (extra != null && extra.length > 0xFFFF) {
 228             throw new IllegalArgumentException("invalid extra field length");
 229         }
 230         this.extra = extra;
 231     }
 232 
 233     /**
 234      * Returns the extra field data for the entry, or null if none.
 235      * @return the extra field data for the entry, or null if none
 236      * @see #setExtra(byte[])
 237      */
 238     public byte[] getExtra() {
 239         return extra;
 240     }
 241 
 242     /**
 243      * Sets the optional comment string for the entry.
 244      *
 245      * <p>ZIP entry comments have maximum length of 0xffff. If the length of the
 246      * specified comment string is greater than 0xFFFF bytes after encoding, only
 247      * the first 0xFFFF bytes are output to the ZIP file entry.
 248      *
 249      * @param comment the comment string
 250      *
 251      * @see #getComment()
 252      */
 253     public void setComment(String comment) {
 254         this.comment = comment;
 255     }
 256 
 257     /**
 258      * Returns the comment string for the entry, or null if none.
 259      * @return the comment string for the entry, or null if none
 260      * @see #setComment(String)
 261      */
 262     public String getComment() {
 263         return comment;
 264     }
 265 
 266     /**
 267      * Returns true if this is a directory entry. A directory entry is
 268      * defined to be one whose name ends with a '/'.
 269      * @return true if this is a directory entry
 270      */
 271     public boolean isDirectory() {
 272         return name.endsWith("/");
 273     }
 274 
 275     /**
 276      * Returns a string representation of the ZIP entry.
 277      */
 278     public String toString() {
 279         return getName();
 280     }
 281 
 282     /**
 283      * Returns the hash code value for this entry.
 284      */
 285     public int hashCode() {
 286         return name.hashCode();
 287     }
 288 
 289     /**
 290      * Returns a copy of this entry.
 291      */
 292     public Object clone() {
 293         try {
 294             ZipEntry e = (ZipEntry)super.clone();
 295             e.extra = (extra == null) ? null : extra.clone();
 296             return e;
 297         } catch (CloneNotSupportedException e) {
 298             // This should never happen, since we are Cloneable
 299             throw new InternalError(e);
 300         }
 301     }
 302 }