1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/* * @(#)ZipEntry.java 1.40 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util.zip; import java.util.Date; /** * This class is used to represent a ZIP file entry. * * @version 1.40, 11/17/05 * @author David Connelly */ public class ZipEntry implements ZipConstants, Cloneable { String name; // entry name long time = -1; // modification time (in DOS time) long crc = -1; // crc-32 of entry data long size = -1; // uncompressed size of entry data long csize = -1; // compressed size of entry data int method = -1; // compression method byte[] extra; // optional extra field data for entry String comment; // optional comment string for entry /** * Compression method for uncompressed entries. */ public static final int STORED = 0; /** * Compression method for compressed (deflated) entries. */ public static final int DEFLATED = 8; static { /* Zip library is loaded from System.initializeSystemClass */ initIDs(); } private static native void initIDs(); /** * Creates a new zip entry with the specified name. * * @param name the entry name * @exception NullPointerException if the entry name is null * @exception IllegalArgumentException if the entry name is longer than * 0xFFFF bytes */ public ZipEntry(String name) { if (name == null) { throw new NullPointerException(); } if (name.length() > 0xFFFF) { throw new IllegalArgumentException("entry name too long"); } this.name = name; } /** * Creates a new zip entry with fields taken from the specified * zip entry. * @param e a zip Entry object */ public ZipEntry(ZipEntry e) { name = e.name; time = e.time; crc = e.crc; size = e.size; csize = e.csize; method = e.method; extra = e.extra; comment = e.comment; } /* * Creates a new zip entry for the given name with fields initialized * from the specified jzentry data. */ ZipEntry(String name, long jzentry) { this.name = name; initFields(jzentry); } private native void initFields(long jzentry); /* * Creates a new zip entry with fields initialized from the specified * jzentry data. */ ZipEntry(long jzentry) { initFields(jzentry); } /** * Returns the name of the entry. * @return the name of the entry */ public String getName() { return name; } /** * Sets the modification time of the entry. * @param time the entry modification time in number of milliseconds * since the epoch * @see #getTime() */ public void setTime(long time) { this.time = javaToDosTime(time); } /** * Returns the modification time of the entry, or -1 if not specified. * @return the modification time of the entry, or -1 if not specified * @see #setTime(long) */ public long getTime() { return time != -1 ? dosToJavaTime(time) : -1; } /** * Sets the uncompressed size of the entry data. * @param size the uncompressed size in bytes * @exception IllegalArgumentException if the specified size is less * than 0 or greater than 0xFFFFFFFF bytes * @see #getSize() */ public void setSize(long size) { if (size < 0 || size > 0xFFFFFFFFL) { throw new IllegalArgumentException("invalid entry size"); } this.size = size; } /** * Returns the uncompressed size of the entry data, or -1 if not known. * @return the uncompressed size of the entry data, or -1 if not known * @see #setSize(long) */ public long getSize() { return size; } /** * Returns the size of the compressed entry data, or -1 if not known. * In the case of a stored entry, the compressed size will be the same * as the uncompressed size of the entry. * @return the size of the compressed entry data, or -1 if not known * @see #setCompressedSize(long) */ public long getCompressedSize() { return csize; } /** * Sets the size of the compressed entry data. * @param csize the compressed size to set to * @see #getCompressedSize() */ public void setCompressedSize(long csize) { this.csize = csize; } /** * Sets the CRC-32 checksum of the uncompressed entry data. * @param crc the CRC-32 value * @exception IllegalArgumentException if the specified CRC-32 value is * less than 0 or greater than 0xFFFFFFFF * @see #getCrc() */ public void setCrc(long crc) { if (crc < 0 || crc > 0xFFFFFFFFL) { throw new IllegalArgumentException("invalid entry crc-32"); } this.crc = crc; } /** * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if * not known. * @return the CRC-32 checksum of the uncompressed entry data, or -1 if * not known * @see #setCrc(long) */ public long getCrc() { return crc; } /** * Sets the compression method for the entry. * @param method the compression method, either STORED or DEFLATED * @exception IllegalArgumentException if the specified compression * method is invalid * @see #getMethod() */ public void setMethod(int method) { if (method != STORED && method != DEFLATED) { throw new IllegalArgumentException("invalid compression method"); } this.method = method; } /** * Returns the compression method of the entry, or -1 if not specified. * @return the compression method of the entry, or -1 if not specified * @see #setMethod(int) */ public int getMethod() { return method; } /** * Sets the optional extra field data for the entry. * @param extra the extra field data bytes * @exception IllegalArgumentException if the length of the specified * extra field data is greater than 0xFFFF bytes * @see #getExtra() */ public void setExtra(byte[] extra) { if (extra != null && extra.length > 0xFFFF) { throw new IllegalArgumentException("invalid extra field length"); } this.extra = extra; } /** * Returns the extra field data for the entry, or null if none. * @return the extra field data for the entry, or null if none * @see #setExtra(byte[]) */ public byte[] getExtra() { return extra; } /** * Sets the optional comment string for the entry. * @param comment the comment string * @exception IllegalArgumentException if the length of the specified * comment string is greater than 0xFFFF bytes * @see #getComment() */ public void setComment(String comment) { if (comment != null && comment.length() > 0xffff/3 && ZipOutputStream.getUTF8Length(comment) > 0xffff) { throw new IllegalArgumentException("invalid entry comment length"); } this.comment = comment; } /** * Returns the comment string for the entry, or null if none. * @return the comment string for the entry, or null if none * @see #setComment(String) */ public String getComment() { return comment; } /** * Returns true if this is a directory entry. A directory entry is * defined to be one whose name ends with a '/'. * @return true if this is a directory entry */ public boolean isDirectory() { return name.endsWith("/"); } /** * Returns a string representation of the ZIP entry. */ public String toString() { return getName(); } /* * Converts DOS time to Java time (number of milliseconds since epoch). */ private static long dosToJavaTime(long dtime) { Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80), (int)(((dtime >> 21) & 0x0f) - 1), (int)((dtime >> 16) & 0x1f), (int)((dtime >> 11) & 0x1f), (int)((dtime >> 5) & 0x3f), (int)((dtime << 1) & 0x3e)); return d.getTime(); } /* * Converts Java time to DOS time. */ private static long javaToDosTime(long time) { Date d = new Date(time); int year = d.getYear() + 1900; if (year < 1980) { return (1 << 21) | (1 << 16); } return (year - 1980) << 25 | (d.getMonth() + 1) << 21 | d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 | d.getSeconds() >> 1; } /** * Returns the hash code value for this entry. */ public int hashCode() { return name.hashCode(); } /** * Returns a copy of this entry. */ public Object clone() { try { ZipEntry e = (ZipEntry)super.clone(); e.extra = (extra == null ? null : (byte[])extra.clone()); return e; } catch (CloneNotSupportedException e) { // This should never happen, since we are Cloneable throw new InternalError(); } } }