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