1 17 18 package net.sourceforge.groboutils.codecoverage.v2.ant.zip; 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.util.Vector ; 23 import java.util.zip.ZipException ; 24 25 32 public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable { 33 34 private static final int PLATFORM_UNIX = 3; 35 private static final int PLATFORM_FAT = 0; 36 37 private int internalAttributes = 0; 38 private int platform = PLATFORM_FAT; 39 private long externalAttributes = 0; 40 private Vector extraFields = new Vector (); 41 private String name = null; 42 43 48 public ZipEntry(String name) { 49 super(name); 50 } 51 52 57 public ZipEntry(java.util.zip.ZipEntry entry) throws ZipException { 58 62 super(entry.getName()); 63 64 setComment(entry.getComment()); 65 setMethod(entry.getMethod()); 66 setTime(entry.getTime()); 67 68 long size = entry.getSize(); 69 if (size > 0) { 70 setSize(size); 71 } 72 long cSize = entry.getCompressedSize(); 73 if (cSize > 0) { 74 setComprSize(cSize); 75 } 76 long crc = entry.getCrc(); 77 if (crc > 0) { 78 setCrc(crc); 79 } 80 81 byte[] extra = entry.getExtra(); 82 if (extra != null) { 83 setExtraFields(ExtraFieldUtils.parse(extra)); 84 } else { 85 setExtra(); 87 } 88 } 89 90 95 public ZipEntry(ZipEntry entry) throws ZipException { 96 this((java.util.zip.ZipEntry ) entry); 97 setInternalAttributes(entry.getInternalAttributes()); 98 setExternalAttributes(entry.getExternalAttributes()); 99 setExtraFields(entry.getExtraFields()); 100 } 101 102 105 protected ZipEntry() { 106 super(""); 107 } 108 109 114 public Object clone() { 115 try { 116 ZipEntry e = (ZipEntry) super.clone(); 117 118 e.setName(getName()); 119 e.setComment(getComment()); 120 e.setMethod(getMethod()); 121 e.setTime(getTime()); 122 long size = getSize(); 123 if (size > 0) { 124 e.setSize(size); 125 } 126 long cSize = getCompressedSize(); 127 if (cSize > 0) { 128 e.setComprSize(cSize); 129 } 130 long crc = getCrc(); 131 if (crc > 0) { 132 e.setCrc(crc); 133 } 134 135 e.extraFields = (Vector ) extraFields.clone(); 136 e.setInternalAttributes(getInternalAttributes()); 137 e.setExternalAttributes(getExternalAttributes()); 138 e.setExtraFields(getExtraFields()); 139 return e; 140 } catch (Throwable t) { 141 return null; 145 } 146 } 147 148 153 public int getInternalAttributes() { 154 return internalAttributes; 155 } 156 157 162 public void setInternalAttributes(int value) { 163 internalAttributes = value; 164 } 165 166 171 public long getExternalAttributes() { 172 return externalAttributes; 173 } 174 175 180 public void setExternalAttributes(long value) { 181 externalAttributes = value; 182 } 183 184 190 public void setUnixMode(int mode) { 191 setExternalAttributes((mode << 16) 192 | ((mode & 0200) == 0 ? 1 : 0) 194 | (isDirectory() ? 0x10 : 0)); 196 platform = PLATFORM_UNIX; 197 } 198 199 204 public int getUnixMode() { 205 return (int) ((getExternalAttributes() >> 16) & 0xFFFF); 206 } 207 208 217 public int getPlatform() { 218 return platform; 219 } 220 221 224 protected void setPlatform(int platform) { 225 this.platform = platform; 226 } 227 228 233 public void setExtraFields(ZipExtraField[] fields) { 234 extraFields.removeAllElements(); 235 for (int i = 0; i < fields.length; i++) { 236 extraFields.addElement(fields[i]); 237 } 238 setExtra(); 239 } 240 241 246 public ZipExtraField[] getExtraFields() { 247 ZipExtraField[] result = new ZipExtraField[extraFields.size()]; 248 extraFields.copyInto(result); 249 return result; 250 } 251 252 258 public void addExtraField(ZipExtraField ze) { 259 ZipShort type = ze.getHeaderId(); 260 boolean done = false; 261 for (int i = 0; !done && i < extraFields.size(); i++) { 262 if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) { 263 extraFields.setElementAt(ze, i); 264 done = true; 265 } 266 } 267 if (!done) { 268 extraFields.addElement(ze); 269 } 270 setExtra(); 271 } 272 273 278 public void removeExtraField(ZipShort type) { 279 boolean done = false; 280 for (int i = 0; !done && i < extraFields.size(); i++) { 281 if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) { 282 extraFields.removeElementAt(i); 283 done = true; 284 } 285 } 286 if (!done) { 287 throw new java.util.NoSuchElementException (); 288 } 289 setExtra(); 290 } 291 292 297 public void setExtra(byte[] extra) throws RuntimeException { 298 try { 299 setExtraFields(ExtraFieldUtils.parse(extra)); 300 } catch (Exception e) { 301 throw new RuntimeException (e.getMessage()); 302 } 303 } 304 305 313 protected void setExtra() { 314 super.setExtra(ExtraFieldUtils.mergeLocalFileDataData(getExtraFields())); 315 } 316 317 322 public byte[] getLocalFileDataExtra() { 323 byte[] extra = getExtra(); 324 return extra != null ? extra : new byte[0]; 325 } 326 327 332 public byte[] getCentralDirectoryExtra() { 333 return ExtraFieldUtils.mergeCentralDirectoryData(getExtraFields()); 334 } 335 336 341 private Long compressedSize = null; 342 343 351 public void setComprSize(long size) { 352 if (haveSetCompressedSize()) { 353 performSetCompressedSize(this, size); 354 } else { 355 compressedSize = new Long (size); 356 } 357 } 358 359 364 public long getCompressedSize() { 365 if (compressedSize != null) { 366 return compressedSize.longValue(); 368 } 369 return super.getCompressedSize(); 370 } 371 372 375 public String getName() { 376 return name == null ? super.getName() : name; 377 } 378 379 382 public boolean isDirectory() { 383 return getName().endsWith("/"); 384 } 385 386 protected void setName(String name) { 387 this.name = name; 388 } 389 390 395 private static Method setCompressedSizeMethod = null; 396 401 private static Object lockReflection = new Object (); 402 407 private static boolean triedToGetMethod = false; 408 409 414 private static boolean haveSetCompressedSize() { 415 checkSCS(); 416 return setCompressedSizeMethod != null; 417 } 418 419 424 private static void performSetCompressedSize(ZipEntry ze, long size) { 425 Long [] s = {new Long (size)}; 426 try { 427 setCompressedSizeMethod.invoke(ze, s); 428 } catch (InvocationTargetException ite) { 429 Throwable nested = ite.getTargetException(); 430 throw new RuntimeException ("Exception setting the compressed size " 431 + "of " + ze + ": " 432 + nested.getMessage()); 433 } catch (Throwable other) { 434 throw new RuntimeException ("Exception setting the compressed size " 435 + "of " + ze + ": " 436 + other.getMessage()); 437 } 438 } 439 440 445 private static void checkSCS() { 446 if (!triedToGetMethod) { 447 synchronized (lockReflection) { 448 triedToGetMethod = true; 449 try { 450 setCompressedSizeMethod = 451 java.util.zip.ZipEntry .class.getMethod("setCompressedSize", 452 new Class [] {Long.TYPE}); 453 } catch (NoSuchMethodException nse) { 454 } 455 } 456 } 457 } 458 459 } 460 | Popular Tags |