| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.apache.tools.zip; |
| 20 |
|
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.LinkedHashMap; |
| 23 |
import java.util.List; |
| 24 |
import java.util.zip.ZipException; |
| 25 |
|
| 26 |
/** |
| 27 |
* Extension that adds better handling of extra fields and provides |
| 28 |
* access to the internal and external file attributes. |
| 29 |
* |
| 30 |
* <p>The extra data is expected to follow the recommendation of |
| 31 |
* {@link http://www.pkware.com/documents/casestudies/APPNOTE.TXT |
| 32 |
* APPNOTE.txt}:</p> |
| 33 |
* <ul> |
| 34 |
* <li>the extra byte array consists of a sequence of extra fields</li> |
| 35 |
* <li>each extra fields starts by a two byte header id followed by |
| 36 |
* a two byte sequence holding the length of the remainder of |
| 37 |
* data.</li> |
| 38 |
* </ul> |
| 39 |
* |
| 40 |
* <p>Any extra data that cannot be parsed by the rules above will be |
| 41 |
* consumed as "unparseable" extra data and treated differently by the |
| 42 |
* methods of this class. Versions prior to Apache Commons Compress |
| 43 |
* 1.1 would have thrown an exception if any attempt was made to read |
| 44 |
* or write extra data not conforming to the recommendation.</p> |
| 45 |
* |
| 46 |
*/ |
| 47 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
| 48 |
public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable { |
| 49 |
|
| 50 |
public static final int PLATFORM_UNIX = 3; |
| 51 |
public static final int PLATFORM_FAT = 0; |
| 52 |
private static final int SHORT_MASK = 0xFFFF; |
| 53 |
private static final int SHORT_SHIFT = 16; |
| 54 |
|
| 55 |
private int internalAttributes = 0; |
| 56 |
private int platform = PLATFORM_FAT; |
| 57 |
private long externalAttributes = 0; |
| 58 |
private LinkedHashMap/*<ZipShort, ZipExtraField>*/ extraFields = null; |
| 59 |
private UnparseableExtraFieldData unparseableExtra = null; |
| 60 |
private String name = null; |
| 61 |
|
| 62 |
/** |
| 63 |
* Creates a new zip entry with the specified name. |
| 64 |
* @param name the name of the entry |
| 65 |
* @since 1.1 |
| 66 |
*/ |
| 67 |
public ZipEntry(String name) { |
| 68 |
super(name); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Creates a new zip entry with fields taken from the specified zip entry. |
| 73 |
* @param entry the entry to get fields from |
| 74 |
* @since 1.1 |
| 75 |
* @throws ZipException on error |
| 76 |
*/ |
| 77 |
public ZipEntry(java.util.zip.ZipEntry entry) throws ZipException { |
| 78 |
super(entry); |
| 79 |
byte[] extra = entry.getExtra(); |
| 80 |
if (extra != null) { |
| 81 |
setExtraFields(ExtraFieldUtils.parse(extra, true, |
| 82 |
ExtraFieldUtils |
| 83 |
.UnparseableExtraField.READ)); |
| 84 |
} else { |
| 85 |
// initializes extra data to an empty byte array |
| 86 |
setExtra(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Creates a new zip entry with fields taken from the specified zip entry. |
| 92 |
* @param entry the entry to get fields from |
| 93 |
* @throws ZipException on error |
| 94 |
* @since 1.1 |
| 95 |
*/ |
| 96 |
public ZipEntry(ZipEntry entry) throws ZipException { |
| 97 |
this((java.util.zip.ZipEntry) entry); |
| 98 |
setInternalAttributes(entry.getInternalAttributes()); |
| 99 |
setExternalAttributes(entry.getExternalAttributes()); |
| 100 |
setExtraFields(entry.getExtraFields(true)); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @since 1.9 |
| 105 |
*/ |
| 106 |
protected ZipEntry() { |
| 107 |
super(""); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Overwrite clone. |
| 112 |
* @return a cloned copy of this ZipEntry |
| 113 |
* @since 1.1 |
| 114 |
*/ |
| 115 |
public Object clone() { |
| 116 |
ZipEntry e = (ZipEntry) super.clone(); |
| 117 |
|
| 118 |
e.setInternalAttributes(getInternalAttributes()); |
| 119 |
e.setExternalAttributes(getExternalAttributes()); |
| 120 |
e.setExtraFields(getExtraFields(true)); |
| 121 |
return e; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Retrieves the internal file attributes. |
| 126 |
* |
| 127 |
* @return the internal file attributes |
| 128 |
* @since 1.1 |
| 129 |
*/ |
| 130 |
public int getInternalAttributes() { |
| 131 |
return internalAttributes; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Sets the internal file attributes. |
| 136 |
* @param value an <code>int</code> value |
| 137 |
* @since 1.1 |
| 138 |
*/ |
| 139 |
public void setInternalAttributes(int value) { |
| 140 |
internalAttributes = value; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Retrieves the external file attributes. |
| 145 |
* @return the external file attributes |
| 146 |
* @since 1.1 |
| 147 |
*/ |
| 148 |
public long getExternalAttributes() { |
| 149 |
return externalAttributes; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Sets the external file attributes. |
| 154 |
* @param value an <code>long</code> value |
| 155 |
* @since 1.1 |
| 156 |
*/ |
| 157 |
public void setExternalAttributes(long value) { |
| 158 |
externalAttributes = value; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Sets Unix permissions in a way that is understood by Info-Zip's |
| 163 |
* unzip command. |
| 164 |
* @param mode an <code>int</code> value |
| 165 |
* @since Ant 1.5.2 |
| 166 |
*/ |
| 167 |
public void setUnixMode(int mode) { |
| 168 |
// CheckStyle:MagicNumberCheck OFF - no point |
| 169 |
setExternalAttributes((mode << SHORT_SHIFT) |
| 170 |
// MS-DOS read-only attribute |
| 171 |
| ((mode & 0200) == 0 ? 1 : 0) |
| 172 |
// MS-DOS directory flag |
| 173 |
| (isDirectory() ? 0x10 : 0)); |
| 174 |
// CheckStyle:MagicNumberCheck ON |
| 175 |
platform = PLATFORM_UNIX; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Unix permission. |
| 180 |
* @return the unix permissions |
| 181 |
* @since Ant 1.6 |
| 182 |
*/ |
| 183 |
public int getUnixMode() { |
| 184 |
return platform != PLATFORM_UNIX ? 0 : |
| 185 |
(int) ((getExternalAttributes() >> SHORT_SHIFT) & SHORT_MASK); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Platform specification to put into the "version made |
| 190 |
* by" part of the central file header. |
| 191 |
* |
| 192 |
* @return PLATFORM_FAT unless {@link #setUnixMode setUnixMode} |
| 193 |
* has been called, in which case PLATORM_UNIX will be returned. |
| 194 |
* |
| 195 |
* @since Ant 1.5.2 |
| 196 |
*/ |
| 197 |
public int getPlatform() { |
| 198 |
return platform; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Set the platform (UNIX or FAT). |
| 203 |
* @param platform an <code>int</code> value - 0 is FAT, 3 is UNIX |
| 204 |
* @since 1.9 |
| 205 |
*/ |
| 206 |
protected void setPlatform(int platform) { |
| 207 |
this.platform = platform; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Replaces all currently attached extra fields with the new array. |
| 212 |
* @param fields an array of extra fields |
| 213 |
* @since 1.1 |
| 214 |
*/ |
| 215 |
public void setExtraFields(ZipExtraField[] fields) { |
| 216 |
extraFields = new LinkedHashMap(); |
| 217 |
for (int i = 0; i < fields.length; i++) { |
| 218 |
if (fields[i] instanceof UnparseableExtraFieldData) { |
| 219 |
unparseableExtra = (UnparseableExtraFieldData) fields[i]; |
| 220 |
} else { |
| 221 |
extraFields.put(fields[i].getHeaderId(), fields[i]); |
| 222 |
} |
| 223 |
} |
| 224 |
setExtra(); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Retrieves all extra fields that have been parsed successfully. |
| 229 |
* @return an array of the extra fields |
| 230 |
*/ |
| 231 |
public ZipExtraField[] getExtraFields() { |
| 232 |
return getExtraFields(false); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Retrieves extra fields. |
| 237 |
* @param includeUnparseable whether to also return unparseable |
| 238 |
* extra fields as {@link UnparseableExtraFieldData} if such data |
| 239 |
* exists. |
| 240 |
* @return an array of the extra fields |
| 241 |
* @since 1.1 |
| 242 |
*/ |
| 243 |
public ZipExtraField[] getExtraFields(boolean includeUnparseable) { |
| 244 |
if (extraFields == null) { |
| 245 |
return !includeUnparseable || unparseableExtra == null |
| 246 |
? new ZipExtraField[0] |
| 247 |
: new ZipExtraField[] { unparseableExtra }; |
| 248 |
} |
| 249 |
List result = new ArrayList(extraFields.values()); |
| 250 |
if (includeUnparseable && unparseableExtra != null) { |
| 251 |
result.add(unparseableExtra); |
| 252 |
} |
| 253 |
return (ZipExtraField[]) result.toArray(new ZipExtraField[0]); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Adds an extra field - replacing an already present extra field |
| 258 |
* of the same type. |
| 259 |
* |
| 260 |
* <p>If no extra field of the same type exists, the field will be |
| 261 |
* added as last field.</p> |
| 262 |
* @param ze an extra field |
| 263 |
* @since 1.1 |
| 264 |
*/ |
| 265 |
public void addExtraField(ZipExtraField ze) { |
| 266 |
if (ze instanceof UnparseableExtraFieldData) { |
| 267 |
unparseableExtra = (UnparseableExtraFieldData) ze; |
| 268 |
} else { |
| 269 |
if (extraFields == null) { |
| 270 |
extraFields = new LinkedHashMap(); |
| 271 |
} |
| 272 |
extraFields.put(ze.getHeaderId(), ze); |
| 273 |
} |
| 274 |
setExtra(); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Adds an extra field - replacing an already present extra field |
| 279 |
* of the same type. |
| 280 |
* |
| 281 |
* <p>The new extra field will be the first one.</p> |
| 282 |
* @param ze an extra field |
| 283 |
* @since 1.1 |
| 284 |
*/ |
| 285 |
public void addAsFirstExtraField(ZipExtraField ze) { |
| 286 |
if (ze instanceof UnparseableExtraFieldData) { |
| 287 |
unparseableExtra = (UnparseableExtraFieldData) ze; |
| 288 |
} else { |
| 289 |
LinkedHashMap copy = extraFields; |
| 290 |
extraFields = new LinkedHashMap(); |
| 291 |
extraFields.put(ze.getHeaderId(), ze); |
| 292 |
if (copy != null) { |
| 293 |
copy.remove(ze.getHeaderId()); |
| 294 |
extraFields.putAll(copy); |
| 295 |
} |
| 296 |
} |
| 297 |
setExtra(); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Remove an extra field. |
| 302 |
* @param type the type of extra field to remove |
| 303 |
* @since 1.1 |
| 304 |
*/ |
| 305 |
public void removeExtraField(ZipShort type) { |
| 306 |
if (extraFields == null) { |
| 307 |
throw new java.util.NoSuchElementException(); |
| 308 |
} |
| 309 |
if (extraFields.remove(type) == null) { |
| 310 |
throw new java.util.NoSuchElementException(); |
| 311 |
} |
| 312 |
setExtra(); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Removes unparseable extra field data. |
| 317 |
*/ |
| 318 |
public void removeUnparseableExtraFieldData() { |
| 319 |
if (unparseableExtra == null) { |
| 320 |
throw new java.util.NoSuchElementException(); |
| 321 |
} |
| 322 |
unparseableExtra = null; |
| 323 |
setExtra(); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Looks up an extra field by its header id. |
| 328 |
* |
| 329 |
* @return null if no such field exists. |
| 330 |
*/ |
| 331 |
public ZipExtraField getExtraField(ZipShort type) { |
| 332 |
if (extraFields != null) { |
| 333 |
return (ZipExtraField) extraFields.get(type); |
| 334 |
} |
| 335 |
return null; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Looks up extra field data that couldn't be parsed correctly. |
| 340 |
* |
| 341 |
* @return null if no such field exists. |
| 342 |
*/ |
| 343 |
public UnparseableExtraFieldData getUnparseableExtraFieldData() { |
| 344 |
return unparseableExtra; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Parses the given bytes as extra field data and consumes any |
| 349 |
* unparseable data as an {@link UnparseableExtraFieldData} |
| 350 |
* instance. |
| 351 |
* @param extra an array of bytes to be parsed into extra fields |
| 352 |
* @throws RuntimeException if the bytes cannot be parsed |
| 353 |
* @since 1.1 |
| 354 |
* @throws RuntimeException on error |
| 355 |
*/ |
| 356 |
public void setExtra(byte[] extra) throws RuntimeException { |
| 357 |
try { |
| 358 |
ZipExtraField[] local = |
| 359 |
ExtraFieldUtils.parse(extra, true, |
| 360 |
ExtraFieldUtils.UnparseableExtraField.READ); |
| 361 |
mergeExtraFields(local, true); |
| 362 |
} catch (Exception e) { |
| 363 |
// actually this is not be possible as of Ant 1.8.1 |
| 364 |
throw new RuntimeException("Error parsing extra fields for entry: " |
| 365 |
+ getName() + " - " + e.getMessage(), e); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Unfortunately {@link java.util.zip.ZipOutputStream |
| 371 |
* java.util.zip.ZipOutputStream} seems to access the extra data |
| 372 |
* directly, so overriding getExtra doesn't help - we need to |
| 373 |
* modify super's data directly. |
| 374 |
* |
| 375 |
* @since 1.1 |
| 376 |
*/ |
| 377 |
protected void setExtra() { |
| 378 |
super.setExtra(ExtraFieldUtils.mergeLocalFileDataData(getExtraFields(true))); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Sets the central directory part of extra fields. |
| 383 |
*/ |
| 384 |
public void setCentralDirectoryExtra(byte[] b) { |
| 385 |
try { |
| 386 |
ZipExtraField[] central = |
| 387 |
ExtraFieldUtils.parse(b, false, |
| 388 |
ExtraFieldUtils.UnparseableExtraField.READ); |
| 389 |
mergeExtraFields(central, false); |
| 390 |
} catch (Exception e) { |
| 391 |
throw new RuntimeException(e.getMessage(), e); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Retrieves the extra data for the local file data. |
| 397 |
* @return the extra data for local file |
| 398 |
* @since 1.1 |
| 399 |
*/ |
| 400 |
public byte[] getLocalFileDataExtra() { |
| 401 |
byte[] extra = getExtra(); |
| 402 |
return extra != null ? extra : new byte[0]; |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Retrieves the extra data for the central directory. |
| 407 |
* @return the central directory extra data |
| 408 |
* @since 1.1 |
| 409 |
*/ |
| 410 |
public byte[] getCentralDirectoryExtra() { |
| 411 |
return ExtraFieldUtils.mergeCentralDirectoryData(getExtraFields(true)); |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Make this class work in JDK 1.1 like a 1.2 class. |
| 416 |
* |
| 417 |
* <p>This either stores the size for later usage or invokes |
| 418 |
* setCompressedSize via reflection.</p> |
| 419 |
* @param size the size to use |
| 420 |
* @deprecated since 1.7. |
| 421 |
* Use setCompressedSize directly. |
| 422 |
* @since 1.2 |
| 423 |
*/ |
| 424 |
public void setComprSize(long size) { |
| 425 |
setCompressedSize(size); |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Get the name of the entry. |
| 430 |
* @return the entry name |
| 431 |
* @since 1.9 |
| 432 |
*/ |
| 433 |
public String getName() { |
| 434 |
return name == null ? super.getName() : name; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Is this entry a directory? |
| 439 |
* @return true if the entry is a directory |
| 440 |
* @since 1.10 |
| 441 |
*/ |
| 442 |
public boolean isDirectory() { |
| 443 |
return getName().endsWith("/"); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Set the name of the entry. |
| 448 |
* @param name the name to use |
| 449 |
*/ |
| 450 |
protected void setName(String name) { |
| 451 |
this.name = name; |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Get the hashCode of the entry. |
| 456 |
* This uses the name as the hashcode. |
| 457 |
* @return a hashcode. |
| 458 |
* @since Ant 1.7 |
| 459 |
*/ |
| 460 |
public int hashCode() { |
| 461 |
// this method has severe consequences on performance. We cannot rely |
| 462 |
// on the super.hashCode() method since super.getName() always return |
| 463 |
// the empty string in the current implemention (there's no setter) |
| 464 |
// so it is basically draining the performance of a hashmap lookup |
| 465 |
return getName().hashCode(); |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
* The equality method. In this case, the implementation returns 'this == o' |
| 470 |
* which is basically the equals method of the Object class. |
| 471 |
* @param o the object to compare to |
| 472 |
* @return true if this object is the same as <code>o</code> |
| 473 |
* @since Ant 1.7 |
| 474 |
*/ |
| 475 |
public boolean equals(Object o) { |
| 476 |
return (this == o); |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* If there are no extra fields, use the given fields as new extra |
| 481 |
* data - otherwise merge the fields assuming the existing fields |
| 482 |
* and the new fields stem from different locations inside the |
| 483 |
* archive. |
| 484 |
* @param f the extra fields to merge |
| 485 |
* @param local whether the new fields originate from local data |
| 486 |
*/ |
| 487 |
private void mergeExtraFields(ZipExtraField[] f, boolean local) |
| 488 |
throws ZipException { |
| 489 |
if (extraFields == null) { |
| 490 |
setExtraFields(f); |
| 491 |
} else { |
| 492 |
for (int i = 0; i < f.length; i++) { |
| 493 |
ZipExtraField existing; |
| 494 |
if (f[i] instanceof UnparseableExtraFieldData) { |
| 495 |
existing = unparseableExtra; |
| 496 |
} else { |
| 497 |
existing = getExtraField(f[i].getHeaderId()); |
| 498 |
} |
| 499 |
if (existing == null) { |
| 500 |
addExtraField(f[i]); |
| 501 |
} else { |
| 502 |
if (local |
| 503 |
|| !(existing |
| 504 |
instanceof CentralDirectoryParsingZipExtraField)) { |
| 505 |
byte[] b = f[i].getLocalFileDataData(); |
| 506 |
existing.parseFromLocalFileData(b, 0, b.length); |
| 507 |
} else { |
| 508 |
byte[] b = f[i].getCentralDirectoryData(); |
| 509 |
((CentralDirectoryParsingZipExtraField) existing) |
| 510 |
.parseFromCentralDirectoryData(b, 0, b.length); |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
setExtra(); |
| 515 |
} |
| 516 |
} |
| 517 |
} |