1 7 8 package java.util.zip; 9 10 import java.util.Date ; 11 12 18 public 19 class ZipEntry implements ZipConstants , Cloneable { 20 String name; long time = -1; long crc = -1; long size = -1; long csize = -1; int method = -1; byte[] extra; String comment; int flag; int version; long offset; 33 36 public static final int STORED = 0; 37 38 41 public static final int DEFLATED = 8; 42 43 static { 44 45 initIDs(); 46 } 47 48 private static native void initIDs(); 49 50 58 public ZipEntry(String name) { 59 if (name == null) { 60 throw new NullPointerException (); 61 } 62 if (name.length() > 0xFFFF) { 63 throw new IllegalArgumentException ("entry name too long"); 64 } 65 this.name = name; 66 } 67 68 73 public ZipEntry(ZipEntry e) { 74 name = e.name; 75 time = e.time; 76 crc = e.crc; 77 size = e.size; 78 csize = e.csize; 79 method = e.method; 80 extra = e.extra; 81 comment = e.comment; 82 } 83 84 88 ZipEntry(String name, long jzentry) { 89 this.name = name; 90 initFields(jzentry); 91 } 92 93 private native void initFields(long jzentry); 94 95 99 ZipEntry(long jzentry) { 100 initFields(jzentry); 101 } 102 103 107 public String getName() { 108 return name; 109 } 110 111 117 public void setTime(long time) { 118 this.time = javaToDosTime(time); 119 } 120 121 126 public long getTime() { 127 return time != -1 ? dosToJavaTime(time) : -1; 128 } 129 130 137 public void setSize(long size) { 138 if (size < 0 || size > 0xFFFFFFFFL) { 139 throw new IllegalArgumentException ("invalid entry size"); 140 } 141 this.size = size; 142 } 143 144 149 public long getSize() { 150 return size; 151 } 152 153 160 public long getCompressedSize() { 161 return csize; 162 } 163 164 169 public void setCompressedSize(long csize) { 170 this.csize = csize; 171 } 172 173 180 public void setCrc(long crc) { 181 if (crc < 0 || crc > 0xFFFFFFFFL) { 182 throw new IllegalArgumentException ("invalid entry crc-32"); 183 } 184 this.crc = crc; 185 } 186 187 194 public long getCrc() { 195 return crc; 196 } 197 198 205 public void setMethod(int method) { 206 if (method != STORED && method != DEFLATED) { 207 throw new IllegalArgumentException ("invalid compression method"); 208 } 209 this.method = method; 210 } 211 212 217 public int getMethod() { 218 return method; 219 } 220 221 228 public void setExtra(byte[] extra) { 229 if (extra != null && extra.length > 0xFFFF) { 230 throw new IllegalArgumentException ("invalid extra field length"); 231 } 232 this.extra = extra; 233 } 234 235 240 public byte[] getExtra() { 241 return extra; 242 } 243 244 251 public void setComment(String comment) { 252 if (comment != null && comment.length() > 0xffff/3 253 && ZipOutputStream.getUTF8Length(comment) > 0xffff) { 254 throw new IllegalArgumentException ("invalid entry comment length"); 255 } 256 this.comment = comment; 257 } 258 259 264 public String getComment() { 265 return comment; 266 } 267 268 273 public boolean isDirectory() { 274 return name.endsWith("/"); 275 } 276 277 280 public String toString() { 281 return getName(); 282 } 283 284 287 private static long dosToJavaTime(long dtime) { 288 Date d = new Date ((int)(((dtime >> 25) & 0x7f) + 80), 289 (int)(((dtime >> 21) & 0x0f) - 1), 290 (int)((dtime >> 16) & 0x1f), 291 (int)((dtime >> 11) & 0x1f), 292 (int)((dtime >> 5) & 0x3f), 293 (int)((dtime << 1) & 0x3e)); 294 return d.getTime(); 295 } 296 297 300 private static long javaToDosTime(long time) { 301 Date d = new Date (time); 302 int year = d.getYear() + 1900; 303 if (year < 1980) { 304 return (1 << 21) | (1 << 16); 305 } 306 return (year - 1980) << 25 | (d.getMonth() + 1) << 21 | 307 d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 | 308 d.getSeconds() >> 1; 309 } 310 311 314 public int hashCode() { 315 return name.hashCode(); 316 } 317 318 321 public Object clone() { 322 try { 323 ZipEntry e = (ZipEntry )super.clone(); 324 e.extra = (extra == null ? null : (byte[])extra.clone()); 325 return e; 326 } catch (CloneNotSupportedException e) { 327 throw new InternalError (); 329 } 330 } 331 } 332 | Popular Tags |