Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 1 | /* |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 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 |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 8 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 10 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 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 | * |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 17 | */ |
| 18 | package org.apache.commons.compress.archivers.zip; |
| 19 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 20 | import java.util.Vector; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 21 | import java.util.zip.ZipException; |
| 22 | |
| 23 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 24 | * Extension that adds better handling of extra fields and provides |
| 25 | * access to the internal and external file attributes. |
| 26 | * |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 27 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 28 | public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 29 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 30 | public static final int PLATFORM_UNIX = 3; |
| 31 | public static final int PLATFORM_FAT = 0; |
| 32 | private static final int SHORT_MASK = 0xFFFF; |
| 33 | private static final int SHORT_SHIFT = 16; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 34 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 35 | private int internalAttributes = 0; |
| 36 | private int platform = PLATFORM_FAT; |
| 37 | private long externalAttributes = 0; |
| 38 | private Vector/*<ZipExtraField>*/ extraFields = null; |
| 39 | private String name = null; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Creates a new zip entry with the specified name. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 43 | * @param name the name of the entry |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 44 | * @since 1.1 |
| 45 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 46 | public ZipEntry(String name) { |
| 47 | super(name); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates a new zip entry with fields taken from the specified zip entry. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 52 | * @param entry the entry to get fields from |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 53 | * @since 1.1 |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 54 | * @throws ZipException on error |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 55 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 56 | public ZipEntry(java.util.zip.ZipEntry entry) throws ZipException { |
| 57 | super(entry); |
| 58 | byte[] extra = entry.getExtra(); |
| 59 | if (extra != null) { |
| 60 | setExtraFields(ExtraFieldUtils.parse(extra)); |
| 61 | } else { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 62 | // initializes extra data to an empty byte array |
| 63 | setExtra(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates a new zip entry with fields taken from the specified zip entry. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 69 | * @param entry the entry to get fields from |
| 70 | * @throws ZipException on error |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 71 | * @since 1.1 |
| 72 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 73 | public ZipEntry(ZipEntry entry) throws ZipException { |
| 74 | this((java.util.zip.ZipEntry) entry); |
| 75 | setInternalAttributes(entry.getInternalAttributes()); |
| 76 | setExternalAttributes(entry.getExternalAttributes()); |
| 77 | setExtraFields(entry.getExtraFields()); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 81 | * @since 1.9 |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 82 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 83 | protected ZipEntry() { |
| 84 | super(""); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 88 | * Overwrite clone. |
| 89 | * @return a cloned copy of this ZipEntry |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 90 | * @since 1.1 |
| 91 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 92 | public Object clone() { |
| 93 | ZipEntry e = (ZipEntry) super.clone(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 94 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 95 | e.extraFields = extraFields != null ? (Vector) extraFields.clone() : null; |
| 96 | e.setInternalAttributes(getInternalAttributes()); |
| 97 | e.setExternalAttributes(getExternalAttributes()); |
| 98 | e.setExtraFields(getExtraFields()); |
| 99 | return e; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Retrieves the internal file attributes. |
| 104 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 105 | * @return the internal file attributes |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 106 | * @since 1.1 |
| 107 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 108 | public int getInternalAttributes() { |
| 109 | return internalAttributes; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 113 | * Sets the internal file attributes. |
| 114 | * @param value an <code>int</code> value |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 115 | * @since 1.1 |
| 116 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 117 | public void setInternalAttributes(int value) { |
| 118 | internalAttributes = value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 122 | * Retrieves the external file attributes. |
| 123 | * @return the external file attributes |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 124 | * @since 1.1 |
| 125 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 126 | public long getExternalAttributes() { |
| 127 | return externalAttributes; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets the external file attributes. |
| 132 | * @param value an <code>long</code> value |
| 133 | * @since 1.1 |
| 134 | */ |
| 135 | public void setExternalAttributes(long value) { |
| 136 | externalAttributes = value; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets Unix permissions in a way that is understood by Info-Zip's |
| 141 | * unzip command. |
| 142 | * @param mode an <code>int</code> value |
| 143 | * @since Ant 1.5.2 |
| 144 | */ |
| 145 | public void setUnixMode(int mode) { |
| 146 | // CheckStyle:MagicNumberCheck OFF - no point |
| 147 | setExternalAttributes((mode << SHORT_SHIFT) |
| 148 | // MS-DOS read-only attribute |
| 149 | | ((mode & 0200) == 0 ? 1 : 0) |
| 150 | // MS-DOS directory flag |
| 151 | | (isDirectory() ? 0x10 : 0)); |
| 152 | // CheckStyle:MagicNumberCheck ON |
| 153 | platform = PLATFORM_UNIX; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Unix permission. |
| 158 | * @return the unix permissions |
| 159 | * @since Ant 1.6 |
| 160 | */ |
| 161 | public int getUnixMode() { |
| 162 | return platform != PLATFORM_UNIX ? 0 : |
| 163 | (int) ((getExternalAttributes() >> SHORT_SHIFT) & SHORT_MASK); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Platform specification to put into the "version made |
| 168 | * by" part of the central file header. |
| 169 | * |
| 170 | * @return PLATFORM_FAT unless {@link #setUnixMode setUnixMode} |
| 171 | * has been called, in which case PLATORM_UNIX will be returned. |
| 172 | * |
| 173 | * @since Ant 1.5.2 |
| 174 | */ |
| 175 | public int getPlatform() { |
| 176 | return platform; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Set the platform (UNIX or FAT). |
| 181 | * @param platform an <code>int</code> value - 0 is FAT, 3 is UNIX |
| 182 | * @since 1.9 |
| 183 | */ |
| 184 | protected void setPlatform(int platform) { |
| 185 | this.platform = platform; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Replaces all currently attached extra fields with the new array. |
| 190 | * @param fields an array of extra fields |
| 191 | * @since 1.1 |
| 192 | */ |
| 193 | public void setExtraFields(ZipExtraField[] fields) { |
| 194 | extraFields = new Vector(); |
| 195 | for (int i = 0; i < fields.length; i++) { |
| 196 | extraFields.addElement(fields[i]); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 197 | } |
| 198 | setExtra(); |
| 199 | } |
| 200 | |
| 201 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 202 | * Retrieves extra fields. |
| 203 | * @return an array of the extra fields |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 204 | * @since 1.1 |
| 205 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 206 | public ZipExtraField[] getExtraFields() { |
| 207 | if (extraFields == null) { |
| 208 | return new ZipExtraField[0]; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 209 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 210 | ZipExtraField[] result = new ZipExtraField[extraFields.size()]; |
| 211 | extraFields.copyInto(result); |
| 212 | return result; |
| 213 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 214 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 215 | /** |
| 216 | * Adds an extra fields - replacing an already present extra field |
| 217 | * of the same type. |
| 218 | * @param ze an extra field |
| 219 | * @since 1.1 |
| 220 | */ |
| 221 | public void addExtraField(ZipExtraField ze) { |
| 222 | if (extraFields == null) { |
| 223 | extraFields = new Vector(); |
| 224 | } |
| 225 | ZipShort type = ze.getHeaderId(); |
| 226 | boolean done = false; |
| 227 | for (int i = 0, fieldsSize = extraFields.size(); !done && i < fieldsSize; i++) { |
| 228 | if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) { |
| 229 | extraFields.setElementAt(ze, i); |
| 230 | done = true; |
| 231 | } |
| 232 | } |
| 233 | if (!done) { |
| 234 | extraFields.addElement(ze); |
| 235 | } |
| 236 | setExtra(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Remove an extra fields. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 241 | * @param type the type of extra field to remove |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 242 | * @since 1.1 |
| 243 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 244 | public void removeExtraField(ZipShort type) { |
| 245 | if (extraFields == null) { |
| 246 | extraFields = new Vector(); |
| 247 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 248 | boolean done = false; |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 249 | for (int i = 0, fieldsSize = extraFields.size(); !done && i < fieldsSize; i++) { |
| 250 | if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) { |
| 251 | extraFields.removeElementAt(i); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 252 | done = true; |
| 253 | } |
| 254 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 255 | if (!done) { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 256 | throw new java.util.NoSuchElementException(); |
| 257 | } |
| 258 | setExtra(); |
| 259 | } |
| 260 | |
| 261 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 262 | * Throws an Exception if extra data cannot be parsed into extra fields. |
| 263 | * @param extra an array of bytes to be parsed into extra fields |
| 264 | * @throws RuntimeException if the bytes cannot be parsed |
| 265 | * @since 1.1 |
| 266 | * @throws RuntimeException on error |
| 267 | */ |
| 268 | public void setExtra(byte[] extra) throws RuntimeException { |
| 269 | try { |
| 270 | setExtraFields(ExtraFieldUtils.parse(extra)); |
| 271 | } catch (Exception e) { |
| 272 | throw new RuntimeException(e.getMessage()); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 277 | * Unfortunately {@link java.util.zip.ZipOutputStream |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 278 | * java.util.zip.ZipOutputStream} seems to access the extra data |
| 279 | * directly, so overriding getExtra doesn't help - we need to |
| 280 | * modify super's data directly. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 281 | * |
| 282 | * @since 1.1 |
| 283 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 284 | protected void setExtra() { |
| 285 | super.setExtra(ExtraFieldUtils.mergeLocalFileDataData(getExtraFields())); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 286 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 287 | |
| 288 | /** |
| 289 | * Retrieves the extra data for the local file data. |
| 290 | * @return the extra data for local file |
| 291 | * @since 1.1 |
| 292 | */ |
| 293 | public byte[] getLocalFileDataExtra() { |
| 294 | byte[] extra = getExtra(); |
| 295 | return extra != null ? extra : new byte[0]; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Retrieves the extra data for the central directory. |
| 300 | * @return the central directory extra data |
| 301 | * @since 1.1 |
| 302 | */ |
| 303 | public byte[] getCentralDirectoryExtra() { |
| 304 | return ExtraFieldUtils.mergeCentralDirectoryData(getExtraFields()); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Make this class work in JDK 1.1 like a 1.2 class. |
| 309 | * |
| 310 | * <p>This either stores the size for later usage or invokes |
| 311 | * setCompressedSize via reflection.</p> |
| 312 | * @param size the size to use |
| 313 | * @deprecated since 1.7. |
| 314 | * Use setCompressedSize directly. |
| 315 | * @since 1.2 |
| 316 | */ |
| 317 | public void setComprSize(long size) { |
| 318 | setCompressedSize(size); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Get the name of the entry. |
| 323 | * @return the entry name |
| 324 | * @since 1.9 |
| 325 | */ |
| 326 | public String getName() { |
| 327 | return name == null ? super.getName() : name; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Is this entry a directory? |
| 332 | * @return true if the entry is a directory |
| 333 | * @since 1.10 |
| 334 | */ |
| 335 | public boolean isDirectory() { |
| 336 | return getName().endsWith("/"); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Set the name of the entry. |
| 341 | * @param name the name to use |
| 342 | */ |
| 343 | protected void setName(String name) { |
| 344 | this.name = name; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Get the hashCode of the entry. |
| 349 | * This uses the name as the hashcode. |
| 350 | * @return a hashcode. |
| 351 | * @since Ant 1.7 |
| 352 | */ |
| 353 | public int hashCode() { |
| 354 | // this method has severe consequences on performance. We cannot rely |
| 355 | // on the super.hashCode() method since super.getName() always return |
| 356 | // the empty string in the current implemention (there's no setter) |
| 357 | // so it is basically draining the performance of a hashmap lookup |
| 358 | return getName().hashCode(); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * The equality method. In this case, the implementation returns 'this == o' |
| 363 | * which is basically the equals method of the Object class. |
| 364 | * @param o the object to compare to |
| 365 | * @return true if this object is the same as <code>o</code> |
| 366 | * @since Ant 1.7 |
| 367 | */ |
| 368 | public boolean equals(Object o) { |
| 369 | return (this == o); |
| 370 | } |
| 371 | |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 372 | } |