1 18 19 package org.apache.tools.zip; 20 21 import java.util.Vector ; 22 import java.util.zip.ZipException ; 23 24 29 public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable { 30 31 private static final int PLATFORM_UNIX = 3; 32 private static final int PLATFORM_FAT = 0; 33 34 private int internalAttributes = 0; 35 private int platform = PLATFORM_FAT; 36 private long externalAttributes = 0; 37 private Vector extraFields = null; 38 private String name = null; 39 40 45 public ZipEntry(String name) { 46 super(name); 47 } 48 49 55 public ZipEntry(java.util.zip.ZipEntry entry) throws ZipException { 56 super(entry); 57 byte[] extra = entry.getExtra(); 58 if (extra != null) { 59 setExtraFields(ExtraFieldUtils.parse(extra)); 60 } else { 61 setExtra(); 63 } 64 } 65 66 72 public ZipEntry(ZipEntry entry) throws ZipException { 73 this((java.util.zip.ZipEntry ) entry); 74 setInternalAttributes(entry.getInternalAttributes()); 75 setExternalAttributes(entry.getExternalAttributes()); 76 setExtraFields(entry.getExtraFields()); 77 } 78 79 82 protected ZipEntry() { 83 super(""); 84 } 85 86 91 public Object clone() { 92 ZipEntry e = (ZipEntry) super.clone(); 93 94 e.extraFields = extraFields != null ? (Vector ) extraFields.clone() : null; 95 e.setInternalAttributes(getInternalAttributes()); 96 e.setExternalAttributes(getExternalAttributes()); 97 e.setExtraFields(getExtraFields()); 98 return e; 99 } 100 101 107 public int getInternalAttributes() { 108 return internalAttributes; 109 } 110 111 116 public void setInternalAttributes(int value) { 117 internalAttributes = value; 118 } 119 120 125 public long getExternalAttributes() { 126 return externalAttributes; 127 } 128 129 134 public void setExternalAttributes(long value) { 135 externalAttributes = value; 136 } 137 138 144 public void setUnixMode(int mode) { 145 setExternalAttributes((mode << 16) 146 | ((mode & 0200) == 0 ? 1 : 0) 148 | (isDirectory() ? 0x10 : 0)); 150 platform = PLATFORM_UNIX; 151 } 152 153 158 public int getUnixMode() { 159 return (int) ((getExternalAttributes() >> 16) & 0xFFFF); 160 } 161 162 171 public int getPlatform() { 172 return platform; 173 } 174 175 180 protected void setPlatform(int platform) { 181 this.platform = platform; 182 } 183 184 189 public void setExtraFields(ZipExtraField[] fields) { 190 extraFields = new Vector (); 191 for (int i = 0; i < fields.length; i++) { 192 extraFields.addElement(fields[i]); 193 } 194 setExtra(); 195 } 196 197 202 public ZipExtraField[] getExtraFields() { 203 if (extraFields == null) { 204 return new ZipExtraField[0]; 205 } 206 ZipExtraField[] result = new ZipExtraField[extraFields.size()]; 207 extraFields.copyInto(result); 208 return result; 209 } 210 211 217 public void addExtraField(ZipExtraField ze) { 218 if (extraFields == null) { 219 extraFields = new Vector (); 220 } 221 ZipShort type = ze.getHeaderId(); 222 boolean done = false; 223 for (int i = 0, fieldsSize = extraFields.size(); !done && i < fieldsSize; i++) { 224 if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) { 225 extraFields.setElementAt(ze, i); 226 done = true; 227 } 228 } 229 if (!done) { 230 extraFields.addElement(ze); 231 } 232 setExtra(); 233 } 234 235 240 public void removeExtraField(ZipShort type) { 241 if (extraFields == null) { 242 extraFields = new Vector (); 243 } 244 boolean done = false; 245 for (int i = 0, fieldsSize = extraFields.size(); !done && i < fieldsSize; i++) { 246 if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) { 247 extraFields.removeElementAt(i); 248 done = true; 249 } 250 } 251 if (!done) { 252 throw new java.util.NoSuchElementException (); 253 } 254 setExtra(); 255 } 256 257 264 public void setExtra(byte[] extra) throws RuntimeException { 265 try { 266 setExtraFields(ExtraFieldUtils.parse(extra)); 267 } catch (Exception e) { 268 throw new RuntimeException (e.getMessage()); 269 } 270 } 271 272 280 protected void setExtra() { 281 super.setExtra(ExtraFieldUtils.mergeLocalFileDataData(getExtraFields())); 282 } 283 284 289 public byte[] getLocalFileDataExtra() { 290 byte[] extra = getExtra(); 291 return extra != null ? extra : new byte[0]; 292 } 293 294 299 public byte[] getCentralDirectoryExtra() { 300 return ExtraFieldUtils.mergeCentralDirectoryData(getExtraFields()); 301 } 302 303 313 public void setComprSize(long size) { 314 setCompressedSize(size); 315 } 316 317 322 public String getName() { 323 return name == null ? super.getName() : name; 324 } 325 326 331 public boolean isDirectory() { 332 return getName().endsWith("/"); 333 } 334 335 339 protected void setName(String name) { 340 this.name = name; 341 } 342 343 349 public int hashCode() { 350 return getName().hashCode(); 355 } 356 357 364 public boolean equals(Object o) { 365 return (this == o); 366 } 367 368 } 369 | Popular Tags |