Source Home >> Java Source 1.6.0 >> java.util.zip.ZipEntry V 0.09
  • 001/*
  • 002 * @(#)ZipEntry.java 1.40 05/11/17
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.util.zip;
  • 009
  • 010import java.util.Date;
  • 011
  • 012/**
  • 013 * This class is used to represent a ZIP file entry.
  • 014 *
  • 015 * @version 1.40, 11/17/05
  • 016 * @author David Connelly
  • 017 */
  • 018public
  • 019class ZipEntry implements ZipConstants, Cloneable {
  • 020 String name; // entry name
  • 021 long time = -1; // modification time (in DOS time)
  • 022 long crc = -1; // crc-32 of entry data
  • 023 long size = -1; // uncompressed size of entry data
  • 024 long csize = -1; // compressed size of entry data
  • 025 int method = -1; // compression method
  • 026 byte[] extra; // optional extra field data for entry
  • 027 String comment; // optional comment string for entry
  • 028
  • 029 /**
  • 030 * Compression method for uncompressed entries.
  • 031 */
  • 032 public static final int STORED = 0;
  • 033
  • 034 /**
  • 035 * Compression method for compressed (deflated) entries.
  • 036 */
  • 037 public static final int DEFLATED = 8;
  • 038
  • 039 static {
  • 040 /* Zip library is loaded from System.initializeSystemClass */
  • 041 initIDs();
  • 042 }
  • 043
  • 044 private static native void initIDs();
  • 045
  • 046 /**
  • 047 * Creates a new zip entry with the specified name.
  • 048 *
  • 049 * @param name the entry name
  • 050 * @exception NullPointerException if the entry name is null
  • 051 * @exception IllegalArgumentException if the entry name is longer than
  • 052 * 0xFFFF bytes
  • 053 */
  • 054 public ZipEntry(String name) {
  • 055 if (name == null) {
  • 056 throw new NullPointerException();
  • 057 }
  • 058 if (name.length() > 0xFFFF) {
  • 059 throw new IllegalArgumentException("entry name too long");
  • 060 }
  • 061 this.name = name;
  • 062 }
  • 063
  • 064 /**
  • 065 * Creates a new zip entry with fields taken from the specified
  • 066 * zip entry.
  • 067 * @param e a zip Entry object
  • 068 */
  • 069 public ZipEntry(ZipEntry e) {
  • 070 name = e.name;
  • 071 time = e.time;
  • 072 crc = e.crc;
  • 073 size = e.size;
  • 074 csize = e.csize;
  • 075 method = e.method;
  • 076 extra = e.extra;
  • 077 comment = e.comment;
  • 078 }
  • 079
  • 080 /*
  • 081 * Creates a new zip entry for the given name with fields initialized
  • 082 * from the specified jzentry data.
  • 083 */
  • 084 ZipEntry(String name, long jzentry) {
  • 085 this.name = name;
  • 086 initFields(jzentry);
  • 087 }
  • 088
  • 089 private native void initFields(long jzentry);
  • 090
  • 091 /*
  • 092 * Creates a new zip entry with fields initialized from the specified
  • 093 * jzentry data.
  • 094 */
  • 095 ZipEntry(long jzentry) {
  • 096 initFields(jzentry);
  • 097 }
  • 098
  • 099 /**
  • 100 * Returns the name of the entry.
  • 101 * @return the name of the entry
  • 102 */
  • 103 public String getName() {
  • 104 return name;
  • 105 }
  • 106
  • 107 /**
  • 108 * Sets the modification time of the entry.
  • 109 * @param time the entry modification time in number of milliseconds
  • 110 * since the epoch
  • 111 * @see #getTime()
  • 112 */
  • 113 public void setTime(long time) {
  • 114 this.time = javaToDosTime(time);
  • 115 }
  • 116
  • 117 /**
  • 118 * Returns the modification time of the entry, or -1 if not specified.
  • 119 * @return the modification time of the entry, or -1 if not specified
  • 120 * @see #setTime(long)
  • 121 */
  • 122 public long getTime() {
  • 123 return time != -1 ? dosToJavaTime(time) : -1;
  • 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 or greater than 0xFFFFFFFF bytes
  • 131 * @see #getSize()
  • 132 */
  • 133 public void setSize(long size) {
  • 134 if (size < 0 || size > 0xFFFFFFFFL) {
  • 135 throw new IllegalArgumentException("invalid entry size");
  • 136 }
  • 137 this.size = size;
  • 138 }
  • 139
  • 140 /**
  • 141 * Returns the uncompressed size of the entry data, or -1 if not known.
  • 142 * @return the uncompressed size of the entry data, or -1 if not known
  • 143 * @see #setSize(long)
  • 144 */
  • 145 public long getSize() {
  • 146 return size;
  • 147 }
  • 148
  • 149 /**
  • 150 * Returns the size of the compressed entry data, or -1 if not known.
  • 151 * In the case of a stored entry, the compressed size will be the same
  • 152 * as the uncompressed size of the entry.
  • 153 * @return the size of the compressed entry data, or -1 if not known
  • 154 * @see #setCompressedSize(long)
  • 155 */
  • 156 public long getCompressedSize() {
  • 157 return csize;
  • 158 }
  • 159
  • 160 /**
  • 161 * Sets the size of the compressed entry data.
  • 162 * @param csize the compressed size to set to
  • 163 * @see #getCompressedSize()
  • 164 */
  • 165 public void setCompressedSize(long csize) {
  • 166 this.csize = csize;
  • 167 }
  • 168
  • 169 /**
  • 170 * Sets the CRC-32 checksum of the uncompressed entry data.
  • 171 * @param crc the CRC-32 value
  • 172 * @exception IllegalArgumentException if the specified CRC-32 value is
  • 173 * less than 0 or greater than 0xFFFFFFFF
  • 174 * @see #getCrc()
  • 175 */
  • 176 public void setCrc(long crc) {
  • 177 if (crc < 0 || crc > 0xFFFFFFFFL) {
  • 178 throw new IllegalArgumentException("invalid entry crc-32");
  • 179 }
  • 180 this.crc = crc;
  • 181 }
  • 182
  • 183 /**
  • 184 * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
  • 185 * not known.
  • 186 * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
  • 187 * not known
  • 188 * @see #setCrc(long)
  • 189 */
  • 190 public long getCrc() {
  • 191 return crc;
  • 192 }
  • 193
  • 194 /**
  • 195 * Sets the compression method for the entry.
  • 196 * @param method the compression method, either STORED or DEFLATED
  • 197 * @exception IllegalArgumentException if the specified compression
  • 198 * method is invalid
  • 199 * @see #getMethod()
  • 200 */
  • 201 public void setMethod(int method) {
  • 202 if (method != STORED && method != DEFLATED) {
  • 203 throw new IllegalArgumentException("invalid compression method");
  • 204 }
  • 205 this.method = method;
  • 206 }
  • 207
  • 208 /**
  • 209 * Returns the compression method of the entry, or -1 if not specified.
  • 210 * @return the compression method of the entry, or -1 if not specified
  • 211 * @see #setMethod(int)
  • 212 */
  • 213 public int getMethod() {
  • 214 return method;
  • 215 }
  • 216
  • 217 /**
  • 218 * Sets the optional extra field data for the entry.
  • 219 * @param extra the extra field data bytes
  • 220 * @exception IllegalArgumentException if the length of the specified
  • 221 * extra field data is greater than 0xFFFF bytes
  • 222 * @see #getExtra()
  • 223 */
  • 224 public void setExtra(byte[] extra) {
  • 225 if (extra != null && extra.length > 0xFFFF) {
  • 226 throw new IllegalArgumentException("invalid extra field length");
  • 227 }
  • 228 this.extra = extra;
  • 229 }
  • 230
  • 231 /**
  • 232 * Returns the extra field data for the entry, or null if none.
  • 233 * @return the extra field data for the entry, or null if none
  • 234 * @see #setExtra(byte[])
  • 235 */
  • 236 public byte[] getExtra() {
  • 237 return extra;
  • 238 }
  • 239
  • 240 /**
  • 241 * Sets the optional comment string for the entry.
  • 242 * @param comment the comment string
  • 243 * @exception IllegalArgumentException if the length of the specified
  • 244 * comment string is greater than 0xFFFF bytes
  • 245 * @see #getComment()
  • 246 */
  • 247 public void setComment(String comment) {
  • 248 if (comment != null && comment.length() > 0xffff/3
  • 249 && ZipOutputStream.getUTF8Length(comment) > 0xffff) {
  • 250 throw new IllegalArgumentException("invalid entry comment length");
  • 251 }
  • 252 this.comment = comment;
  • 253 }
  • 254
  • 255 /**
  • 256 * Returns the comment string for the entry, or null if none.
  • 257 * @return the comment string for the entry, or null if none
  • 258 * @see #setComment(String)
  • 259 */
  • 260 public String getComment() {
  • 261 return comment;
  • 262 }
  • 263
  • 264 /**
  • 265 * Returns true if this is a directory entry. A directory entry is
  • 266 * defined to be one whose name ends with a '/'.
  • 267 * @return true if this is a directory entry
  • 268 */
  • 269 public boolean isDirectory() {
  • 270 return name.endsWith("/");
  • 271 }
  • 272
  • 273 /**
  • 274 * Returns a string representation of the ZIP entry.
  • 275 */
  • 276 public String toString() {
  • 277 return getName();
  • 278 }
  • 279
  • 280 /*
  • 281 * Converts DOS time to Java time (number of milliseconds since epoch).
  • 282 */
  • 283 private static long dosToJavaTime(long dtime) {
  • 284 Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
  • 285 (int)(((dtime >> 21) & 0x0f) - 1),
  • 286 (int)((dtime >> 16) & 0x1f),
  • 287 (int)((dtime >> 11) & 0x1f),
  • 288 (int)((dtime >> 5) & 0x3f),
  • 289 (int)((dtime << 1) & 0x3e));
  • 290 return d.getTime();
  • 291 }
  • 292
  • 293 /*
  • 294 * Converts Java time to DOS time.
  • 295 */
  • 296 private static long javaToDosTime(long time) {
  • 297 Date d = new Date(time);
  • 298 int year = d.getYear() + 1900;
  • 299 if (year < 1980) {
  • 300 return (1 << 21) | (1 << 16);
  • 301 }
  • 302 return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
  • 303 d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
  • 304 d.getSeconds() >> 1;
  • 305 }
  • 306
  • 307 /**
  • 308 * Returns the hash code value for this entry.
  • 309 */
  • 310 public int hashCode() {
  • 311 return name.hashCode();
  • 312 }
  • 313
  • 314 /**
  • 315 * Returns a copy of this entry.
  • 316 */
  • 317 public Object clone() {
  • 318 try {
  • 319 ZipEntry e = (ZipEntry)super.clone();
  • 320 e.extra = (extra == null ? null : (byte[])extra.clone());
  • 321 return e;
  • 322 } catch (CloneNotSupportedException e) {
  • 323 // This should never happen, since we are Cloneable
  • 324 throw new InternalError();
  • 325 }
  • 326 }
  • 327}

文件:ZipEntry.java
包名:java.util.zip
类名:ZipEntry
继承:
接口:[ZipConstants][Cloneable]