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 | |
Julius Davies | 1120914 | 2013-01-14 21:42:48 +0000 | [diff] [blame] | 20 | import org.apache.commons.compress.archivers.ArchiveEntry; |
Julius Davies | 1120914 | 2013-01-14 21:42:48 +0000 | [diff] [blame] | 21 | |
Sebastian Bazley | fec51a1 | 2009-03-31 00:35:56 +0000 | [diff] [blame] | 22 | import java.io.File; |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 23 | import java.util.ArrayList; |
Stefan Bodewig | 7be9796 | 2010-01-26 05:24:17 +0000 | [diff] [blame] | 24 | import java.util.Arrays; |
Stefan Bodewig | 17ffd7f | 2009-08-01 14:52:15 +0000 | [diff] [blame] | 25 | import java.util.Date; |
Stefan Bodewig | 2ae5e28 | 2009-02-04 08:45:31 +0000 | [diff] [blame] | 26 | import java.util.LinkedHashMap; |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 27 | import java.util.List; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 28 | import java.util.zip.ZipException; |
| 29 | |
| 30 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 31 | * Extension that adds better handling of extra fields and provides |
| 32 | * access to the internal and external file attributes. |
| 33 | * |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 34 | * <p>The extra data is expected to follow the recommendation of |
Sebastian Bazley | 75ecf4f | 2010-03-13 13:33:38 +0000 | [diff] [blame] | 35 | * {@link <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT"> |
| 36 | * APPNOTE.txt</a>}:</p> |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 37 | * <ul> |
| 38 | * <li>the extra byte array consists of a sequence of extra fields</li> |
| 39 | * <li>each extra fields starts by a two byte header id followed by |
| 40 | * a two byte sequence holding the length of the remainder of |
| 41 | * data.</li> |
| 42 | * </ul> |
| 43 | * |
| 44 | * <p>Any extra data that cannot be parsed by the rules above will be |
| 45 | * consumed as "unparseable" extra data and treated differently by the |
| 46 | * methods of this class. Versions prior to Apache Commons Compress |
| 47 | * 1.1 would have thrown an exception if any attempt was made to read |
| 48 | * or write extra data not conforming to the recommendation.</p> |
| 49 | * |
Sebastian Bazley | 99870ef | 2009-03-28 00:04:36 +0000 | [diff] [blame] | 50 | * @NotThreadSafe |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 51 | */ |
Stefan Bodewig | 463d6eb | 2009-02-06 17:11:40 +0000 | [diff] [blame] | 52 | public class ZipArchiveEntry extends java.util.zip.ZipEntry |
Gary D. Gregory | 9f0db87 | 2012-04-01 13:14:28 +0000 | [diff] [blame] | 53 | implements ArchiveEntry { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 54 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 55 | public static final int PLATFORM_UNIX = 3; |
| 56 | public static final int PLATFORM_FAT = 0; |
| 57 | private static final int SHORT_MASK = 0xFFFF; |
| 58 | private static final int SHORT_SHIFT = 16; |
Stefan Bodewig | a2f978e | 2013-01-05 19:28:42 +0000 | [diff] [blame] | 59 | private static final byte[] EMPTY = new byte[0]; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 60 | |
Jukka Zitting | 7b125a3 | 2009-12-13 18:31:55 +0000 | [diff] [blame] | 61 | /** |
| 62 | * The {@link java.util.zip.ZipEntry} base class only supports |
| 63 | * the compression methods STORED and DEFLATED. We override the |
| 64 | * field so that any compression methods can be used. |
| 65 | * <p> |
| 66 | * The default value -1 means that the method has not been specified. |
| 67 | * |
| 68 | * @see <a href="https://issues.apache.org/jira/browse/COMPRESS-93" |
| 69 | * >COMPRESS-93</a> |
| 70 | */ |
| 71 | private int method = -1; |
| 72 | |
Stefan Bodewig | d7d90da | 2011-07-21 13:18:36 +0000 | [diff] [blame] | 73 | /** |
| 74 | * The {@link java.util.zip.ZipEntry#setSize} method in the base |
| 75 | * class throws an IllegalArgumentException if the size is bigger |
| 76 | * than 2GB for Java versions < 7. Need to keep our own size |
| 77 | * information for Zip64 support. |
| 78 | */ |
| 79 | private long size = SIZE_UNKNOWN; |
| 80 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 81 | private int internalAttributes = 0; |
| 82 | private int platform = PLATFORM_FAT; |
| 83 | private long externalAttributes = 0; |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 84 | private LinkedHashMap<ZipShort, ZipExtraField> extraFields = null; |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 85 | private UnparseableExtraFieldData unparseableExtra = null; |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 86 | private String name = null; |
Stefan Bodewig | 934a96e | 2011-07-23 04:33:44 +0000 | [diff] [blame] | 87 | private byte[] rawName = null; |
Stefan Bodewig | 62cb9c4 | 2010-03-12 16:06:20 +0000 | [diff] [blame] | 88 | private GeneralPurposeBit gpb = new GeneralPurposeBit(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Creates a new zip entry with the specified name. |
Stefan Bodewig | 9b4b477 | 2010-04-19 15:18:05 +0000 | [diff] [blame] | 92 | * |
| 93 | * <p>Assumes the entry represents a directory if and only if the |
| 94 | * name ends with a forward slash "/".</p> |
| 95 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 96 | * @param name the name of the entry |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 97 | */ |
Stefan Bodewig | 463d6eb | 2009-02-06 17:11:40 +0000 | [diff] [blame] | 98 | public ZipArchiveEntry(String name) { |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 99 | super(name); |
Stefan Bodewig | 5066e9a | 2010-01-07 09:34:55 +0000 | [diff] [blame] | 100 | setName(name); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Creates a new zip entry with fields taken from the specified zip entry. |
Stefan Bodewig | 9b4b477 | 2010-04-19 15:18:05 +0000 | [diff] [blame] | 105 | * |
| 106 | * <p>Assumes the entry represents a directory if and only if the |
| 107 | * name ends with a forward slash "/".</p> |
| 108 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 109 | * @param entry the entry to get fields from |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 110 | * @throws ZipException on error |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 111 | */ |
Stefan Bodewig | 463d6eb | 2009-02-06 17:11:40 +0000 | [diff] [blame] | 112 | public ZipArchiveEntry(java.util.zip.ZipEntry entry) throws ZipException { |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 113 | super(entry); |
Stefan Bodewig | 008ca94 | 2009-03-26 22:29:59 +0000 | [diff] [blame] | 114 | setName(entry.getName()); |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 115 | byte[] extra = entry.getExtra(); |
| 116 | if (extra != null) { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 117 | setExtraFields(ExtraFieldUtils.parse(extra, true, |
| 118 | ExtraFieldUtils |
| 119 | .UnparseableExtraField.READ)); |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 120 | } else { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 121 | // initializes extra data to an empty byte array |
| 122 | setExtra(); |
| 123 | } |
Jukka Zitting | 7b125a3 | 2009-12-13 18:31:55 +0000 | [diff] [blame] | 124 | setMethod(entry.getMethod()); |
Stefan Bodewig | d7d90da | 2011-07-21 13:18:36 +0000 | [diff] [blame] | 125 | this.size = entry.getSize(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Creates a new zip entry with fields taken from the specified zip entry. |
Stefan Bodewig | 9b4b477 | 2010-04-19 15:18:05 +0000 | [diff] [blame] | 130 | * |
| 131 | * <p>Assumes the entry represents a directory if and only if the |
| 132 | * name ends with a forward slash "/".</p> |
| 133 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 134 | * @param entry the entry to get fields from |
| 135 | * @throws ZipException on error |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 136 | */ |
Stefan Bodewig | 463d6eb | 2009-02-06 17:11:40 +0000 | [diff] [blame] | 137 | public ZipArchiveEntry(ZipArchiveEntry entry) throws ZipException { |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 138 | this((java.util.zip.ZipEntry) entry); |
| 139 | setInternalAttributes(entry.getInternalAttributes()); |
| 140 | setExternalAttributes(entry.getExternalAttributes()); |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 141 | setExtraFields(entry.getExtraFields(true)); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 145 | */ |
Stefan Bodewig | 463d6eb | 2009-02-06 17:11:40 +0000 | [diff] [blame] | 146 | protected ZipArchiveEntry() { |
Stefan Bodewig | 5066e9a | 2010-01-07 09:34:55 +0000 | [diff] [blame] | 147 | this(""); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Stefan Bodewig | 9b4b477 | 2010-04-19 15:18:05 +0000 | [diff] [blame] | 150 | /** |
| 151 | * Creates a new zip entry taking some information from the given |
| 152 | * file and using the provided name. |
| 153 | * |
| 154 | * <p>The name will be adjusted to end with a forward slash "/" if |
| 155 | * the file is a directory. If the file is not a directory a |
| 156 | * potential trailing forward slash will be stripped from the |
| 157 | * entry name.</p> |
| 158 | */ |
Sebastian Bazley | fec51a1 | 2009-03-31 00:35:56 +0000 | [diff] [blame] | 159 | public ZipArchiveEntry(File inputFile, String entryName) { |
Stefan Bodewig | a180778 | 2009-08-01 19:17:58 +0000 | [diff] [blame] | 160 | this(inputFile.isDirectory() && !entryName.endsWith("/") ? |
| 161 | entryName + "/" : entryName); |
Sebastian Bazley | 2de1b50 | 2009-03-31 13:01:25 +0000 | [diff] [blame] | 162 | if (inputFile.isFile()){ |
| 163 | setSize(inputFile.length()); |
| 164 | } |
Sebastian Bazley | e46c02b | 2009-03-31 10:35:53 +0000 | [diff] [blame] | 165 | setTime(inputFile.lastModified()); |
Sebastian Bazley | fec51a1 | 2009-03-31 00:35:56 +0000 | [diff] [blame] | 166 | // TODO are there any other fields we can set here? |
| 167 | } |
| 168 | |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 169 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 170 | * Overwrite clone. |
Stefan Bodewig | 463d6eb | 2009-02-06 17:11:40 +0000 | [diff] [blame] | 171 | * @return a cloned copy of this ZipArchiveEntry |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 172 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 173 | @Override |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 174 | public Object clone() { |
Stefan Bodewig | 463d6eb | 2009-02-06 17:11:40 +0000 | [diff] [blame] | 175 | ZipArchiveEntry e = (ZipArchiveEntry) super.clone(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 176 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 177 | e.setInternalAttributes(getInternalAttributes()); |
| 178 | e.setExternalAttributes(getExternalAttributes()); |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 179 | e.setExtraFields(getExtraFields(true)); |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 180 | return e; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Jukka Zitting | 7b125a3 | 2009-12-13 18:31:55 +0000 | [diff] [blame] | 184 | * Returns the compression method of this entry, or -1 if the |
| 185 | * compression method has not been specified. |
| 186 | * |
| 187 | * @return compression method |
Sebastian Bazley | 6209f81 | 2010-05-10 17:36:40 +0000 | [diff] [blame] | 188 | * |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 189 | * @since 1.1 |
Jukka Zitting | 7b125a3 | 2009-12-13 18:31:55 +0000 | [diff] [blame] | 190 | */ |
Sebastian Bazley | fb06e3a | 2011-08-15 15:10:46 +0000 | [diff] [blame] | 191 | @Override |
Jukka Zitting | 7b125a3 | 2009-12-13 18:31:55 +0000 | [diff] [blame] | 192 | public int getMethod() { |
| 193 | return method; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Sets the compression method of this entry. |
| 198 | * |
| 199 | * @param method compression method |
Sebastian Bazley | 6209f81 | 2010-05-10 17:36:40 +0000 | [diff] [blame] | 200 | * |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 201 | * @since 1.1 |
Jukka Zitting | 7b125a3 | 2009-12-13 18:31:55 +0000 | [diff] [blame] | 202 | */ |
Sebastian Bazley | fb06e3a | 2011-08-15 15:10:46 +0000 | [diff] [blame] | 203 | @Override |
Jukka Zitting | 7b125a3 | 2009-12-13 18:31:55 +0000 | [diff] [blame] | 204 | public void setMethod(int method) { |
| 205 | if (method < 0) { |
| 206 | throw new IllegalArgumentException( |
| 207 | "ZIP compression method can not be negative: " + method); |
| 208 | } |
| 209 | this.method = method; |
| 210 | } |
| 211 | |
| 212 | /** |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 213 | * Retrieves the internal file attributes. |
| 214 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 215 | * @return the internal file attributes |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 216 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 217 | public int getInternalAttributes() { |
| 218 | return internalAttributes; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 222 | * Sets the internal file attributes. |
| 223 | * @param value an <code>int</code> value |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 224 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 225 | public void setInternalAttributes(int value) { |
| 226 | internalAttributes = value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 230 | * Retrieves the external file attributes. |
| 231 | * @return the external file attributes |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 232 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 233 | public long getExternalAttributes() { |
| 234 | return externalAttributes; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Sets the external file attributes. |
| 239 | * @param value an <code>long</code> value |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 240 | */ |
| 241 | public void setExternalAttributes(long value) { |
| 242 | externalAttributes = value; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Sets Unix permissions in a way that is understood by Info-Zip's |
| 247 | * unzip command. |
| 248 | * @param mode an <code>int</code> value |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 249 | */ |
| 250 | public void setUnixMode(int mode) { |
| 251 | // CheckStyle:MagicNumberCheck OFF - no point |
| 252 | setExternalAttributes((mode << SHORT_SHIFT) |
| 253 | // MS-DOS read-only attribute |
| 254 | | ((mode & 0200) == 0 ? 1 : 0) |
| 255 | // MS-DOS directory flag |
| 256 | | (isDirectory() ? 0x10 : 0)); |
| 257 | // CheckStyle:MagicNumberCheck ON |
| 258 | platform = PLATFORM_UNIX; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Unix permission. |
| 263 | * @return the unix permissions |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 264 | */ |
| 265 | public int getUnixMode() { |
| 266 | return platform != PLATFORM_UNIX ? 0 : |
| 267 | (int) ((getExternalAttributes() >> SHORT_SHIFT) & SHORT_MASK); |
| 268 | } |
| 269 | |
| 270 | /** |
Julius Davies | 1120914 | 2013-01-14 21:42:48 +0000 | [diff] [blame] | 271 | * Returns true if this entry represents a unix symlink, |
| 272 | * in which case the entry's content contains the target path |
| 273 | * for the symlink. |
| 274 | * |
Stefan Bodewig | c1fd317 | 2013-03-10 16:37:16 +0000 | [diff] [blame] | 275 | * @since 1.5 |
Julius Davies | 1120914 | 2013-01-14 21:42:48 +0000 | [diff] [blame] | 276 | * @return true if the entry represents a unix symlink, false otherwise. |
| 277 | */ |
| 278 | public boolean isUnixSymlink() { |
| 279 | return (getUnixMode() & UnixStat.LINK_FLAG) == UnixStat.LINK_FLAG; |
| 280 | } |
| 281 | |
| 282 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 283 | * Platform specification to put into the "version made |
| 284 | * by" part of the central file header. |
| 285 | * |
| 286 | * @return PLATFORM_FAT unless {@link #setUnixMode setUnixMode} |
Stefan Bodewig | 306c3c8 | 2012-06-04 04:43:01 +0000 | [diff] [blame] | 287 | * has been called, in which case PLATFORM_UNIX will be returned. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 288 | */ |
| 289 | public int getPlatform() { |
| 290 | return platform; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Set the platform (UNIX or FAT). |
| 295 | * @param platform an <code>int</code> value - 0 is FAT, 3 is UNIX |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 296 | */ |
| 297 | protected void setPlatform(int platform) { |
| 298 | this.platform = platform; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Replaces all currently attached extra fields with the new array. |
| 303 | * @param fields an array of extra fields |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 304 | */ |
| 305 | public void setExtraFields(ZipExtraField[] fields) { |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 306 | extraFields = new LinkedHashMap<ZipShort, ZipExtraField>(); |
Gary D. Gregory | b2a9dd0 | 2012-04-01 13:19:03 +0000 | [diff] [blame] | 307 | for (ZipExtraField field : fields) { |
| 308 | if (field instanceof UnparseableExtraFieldData) { |
| 309 | unparseableExtra = (UnparseableExtraFieldData) field; |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 310 | } else { |
Gary D. Gregory | b2a9dd0 | 2012-04-01 13:19:03 +0000 | [diff] [blame] | 311 | extraFields.put(field.getHeaderId(), field); |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 312 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 313 | } |
| 314 | setExtra(); |
| 315 | } |
| 316 | |
| 317 | /** |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 318 | * Retrieves all extra fields that have been parsed successfully. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 319 | * @return an array of the extra fields |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 320 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 321 | public ZipExtraField[] getExtraFields() { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 322 | return getExtraFields(false); |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 323 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 324 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 325 | /** |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 326 | * Retrieves extra fields. |
| 327 | * @param includeUnparseable whether to also return unparseable |
| 328 | * extra fields as {@link UnparseableExtraFieldData} if such data |
| 329 | * exists. |
| 330 | * @return an array of the extra fields |
Sebastian Bazley | 6209f81 | 2010-05-10 17:36:40 +0000 | [diff] [blame] | 331 | * |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 332 | * @since 1.1 |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 333 | */ |
| 334 | public ZipExtraField[] getExtraFields(boolean includeUnparseable) { |
| 335 | if (extraFields == null) { |
| 336 | return !includeUnparseable || unparseableExtra == null |
| 337 | ? new ZipExtraField[0] |
| 338 | : new ZipExtraField[] { unparseableExtra }; |
| 339 | } |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 340 | List<ZipExtraField> result = |
| 341 | new ArrayList<ZipExtraField>(extraFields.values()); |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 342 | if (includeUnparseable && unparseableExtra != null) { |
| 343 | result.add(unparseableExtra); |
| 344 | } |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 345 | return result.toArray(new ZipExtraField[0]); |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Adds an extra field - replacing an already present extra field |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 350 | * of the same type. |
Stefan Bodewig | 1903db4 | 2009-02-18 15:52:42 +0000 | [diff] [blame] | 351 | * |
| 352 | * <p>If no extra field of the same type exists, the field will be |
| 353 | * added as last field.</p> |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 354 | * @param ze an extra field |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 355 | */ |
| 356 | public void addExtraField(ZipExtraField ze) { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 357 | if (ze instanceof UnparseableExtraFieldData) { |
| 358 | unparseableExtra = (UnparseableExtraFieldData) ze; |
| 359 | } else { |
Stefan Bodewig | 3c5ab4f | 2010-02-16 14:00:56 +0000 | [diff] [blame] | 360 | if (extraFields == null) { |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 361 | extraFields = new LinkedHashMap<ZipShort, ZipExtraField>(); |
Stefan Bodewig | 3c5ab4f | 2010-02-16 14:00:56 +0000 | [diff] [blame] | 362 | } |
| 363 | extraFields.put(ze.getHeaderId(), ze); |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 364 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 365 | setExtra(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 369 | * Adds an extra field - replacing an already present extra field |
Stefan Bodewig | 1903db4 | 2009-02-18 15:52:42 +0000 | [diff] [blame] | 370 | * of the same type. |
| 371 | * |
| 372 | * <p>The new extra field will be the first one.</p> |
| 373 | * @param ze an extra field |
Stefan Bodewig | 1903db4 | 2009-02-18 15:52:42 +0000 | [diff] [blame] | 374 | */ |
| 375 | public void addAsFirstExtraField(ZipExtraField ze) { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 376 | if (ze instanceof UnparseableExtraFieldData) { |
| 377 | unparseableExtra = (UnparseableExtraFieldData) ze; |
| 378 | } else { |
Stefan Bodewig | 3553cda | 2011-08-06 13:01:16 +0000 | [diff] [blame] | 379 | LinkedHashMap<ZipShort, ZipExtraField> copy = extraFields; |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 380 | extraFields = new LinkedHashMap<ZipShort, ZipExtraField>(); |
Stefan Bodewig | 3c5ab4f | 2010-02-16 14:00:56 +0000 | [diff] [blame] | 381 | extraFields.put(ze.getHeaderId(), ze); |
| 382 | if (copy != null) { |
| 383 | copy.remove(ze.getHeaderId()); |
| 384 | extraFields.putAll(copy); |
| 385 | } |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 386 | } |
Stefan Bodewig | 1903db4 | 2009-02-18 15:52:42 +0000 | [diff] [blame] | 387 | setExtra(); |
| 388 | } |
| 389 | |
| 390 | /** |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 391 | * Remove an extra field. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 392 | * @param type the type of extra field to remove |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 393 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 394 | public void removeExtraField(ZipShort type) { |
| 395 | if (extraFields == null) { |
Stefan Bodewig | 2ae5e28 | 2009-02-04 08:45:31 +0000 | [diff] [blame] | 396 | throw new java.util.NoSuchElementException(); |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 397 | } |
Stefan Bodewig | 2ae5e28 | 2009-02-04 08:45:31 +0000 | [diff] [blame] | 398 | if (extraFields.remove(type) == null) { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 399 | throw new java.util.NoSuchElementException(); |
| 400 | } |
| 401 | setExtra(); |
| 402 | } |
| 403 | |
| 404 | /** |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 405 | * Removes unparseable extra field data. |
Sebastian Bazley | 6209f81 | 2010-05-10 17:36:40 +0000 | [diff] [blame] | 406 | * |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 407 | * @since 1.1 |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 408 | */ |
| 409 | public void removeUnparseableExtraFieldData() { |
| 410 | if (unparseableExtra == null) { |
| 411 | throw new java.util.NoSuchElementException(); |
| 412 | } |
| 413 | unparseableExtra = null; |
| 414 | setExtra(); |
| 415 | } |
| 416 | |
| 417 | /** |
Stefan Bodewig | 2b5fb91 | 2009-02-25 16:29:00 +0000 | [diff] [blame] | 418 | * Looks up an extra field by its header id. |
| 419 | * |
| 420 | * @return null if no such field exists. |
| 421 | */ |
| 422 | public ZipExtraField getExtraField(ZipShort type) { |
| 423 | if (extraFields != null) { |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 424 | return extraFields.get(type); |
Stefan Bodewig | 2b5fb91 | 2009-02-25 16:29:00 +0000 | [diff] [blame] | 425 | } |
| 426 | return null; |
| 427 | } |
| 428 | |
| 429 | /** |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 430 | * Looks up extra field data that couldn't be parsed correctly. |
| 431 | * |
| 432 | * @return null if no such field exists. |
Sebastian Bazley | 6209f81 | 2010-05-10 17:36:40 +0000 | [diff] [blame] | 433 | * |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 434 | * @since 1.1 |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 435 | */ |
| 436 | public UnparseableExtraFieldData getUnparseableExtraFieldData() { |
| 437 | return unparseableExtra; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Parses the given bytes as extra field data and consumes any |
| 442 | * unparseable data as an {@link UnparseableExtraFieldData} |
| 443 | * instance. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 444 | * @param extra an array of bytes to be parsed into extra fields |
| 445 | * @throws RuntimeException if the bytes cannot be parsed |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 446 | * @throws RuntimeException on error |
| 447 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 448 | @Override |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 449 | public void setExtra(byte[] extra) throws RuntimeException { |
| 450 | try { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 451 | ZipExtraField[] local = |
| 452 | ExtraFieldUtils.parse(extra, true, |
| 453 | ExtraFieldUtils.UnparseableExtraField.READ); |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 454 | mergeExtraFields(local, true); |
Christian Grobmeier | bcebf92 | 2009-04-27 17:22:14 +0000 | [diff] [blame] | 455 | } catch (ZipException e) { |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 456 | // actually this is not possible as of Commons Compress 1.1 |
Stefan Bodewig | 25b5956 | 2010-02-12 11:11:34 +0000 | [diff] [blame] | 457 | throw new RuntimeException("Error parsing extra fields for entry: " |
| 458 | + getName() + " - " + e.getMessage(), e); |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| 462 | /** |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 463 | * Unfortunately {@link java.util.zip.ZipOutputStream |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 464 | * java.util.zip.ZipOutputStream} seems to access the extra data |
| 465 | * directly, so overriding getExtra doesn't help - we need to |
| 466 | * modify super's data directly. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 467 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 468 | protected void setExtra() { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 469 | super.setExtra(ExtraFieldUtils.mergeLocalFileDataData(getExtraFields(true))); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 470 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 471 | |
| 472 | /** |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 473 | * Sets the central directory part of extra fields. |
| 474 | */ |
| 475 | public void setCentralDirectoryExtra(byte[] b) { |
| 476 | try { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 477 | ZipExtraField[] central = |
| 478 | ExtraFieldUtils.parse(b, false, |
| 479 | ExtraFieldUtils.UnparseableExtraField.READ); |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 480 | mergeExtraFields(central, false); |
Christian Grobmeier | bcebf92 | 2009-04-27 17:22:14 +0000 | [diff] [blame] | 481 | } catch (ZipException e) { |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 482 | throw new RuntimeException(e.getMessage(), e); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 487 | * Retrieves the extra data for the local file data. |
| 488 | * @return the extra data for local file |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 489 | */ |
| 490 | public byte[] getLocalFileDataExtra() { |
| 491 | byte[] extra = getExtra(); |
Stefan Bodewig | a2f978e | 2013-01-05 19:28:42 +0000 | [diff] [blame] | 492 | return extra != null ? extra : EMPTY; |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Retrieves the extra data for the central directory. |
| 497 | * @return the central directory extra data |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 498 | */ |
| 499 | public byte[] getCentralDirectoryExtra() { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 500 | return ExtraFieldUtils.mergeCentralDirectoryData(getExtraFields(true)); |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 504 | * Get the name of the entry. |
| 505 | * @return the entry name |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 506 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 507 | @Override |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 508 | public String getName() { |
| 509 | return name == null ? super.getName() : name; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Is this entry a directory? |
| 514 | * @return true if the entry is a directory |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 515 | */ |
Sebastian Bazley | fb06e3a | 2011-08-15 15:10:46 +0000 | [diff] [blame] | 516 | @Override |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 517 | public boolean isDirectory() { |
| 518 | return getName().endsWith("/"); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Set the name of the entry. |
| 523 | * @param name the name to use |
| 524 | */ |
| 525 | protected void setName(String name) { |
Stefan Bodewig | c75c10f | 2012-02-28 05:00:26 +0000 | [diff] [blame] | 526 | if (name != null && getPlatform() == PLATFORM_FAT |
| 527 | && name.indexOf("/") == -1) { |
| 528 | name = name.replace('\\', '/'); |
| 529 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 530 | this.name = name; |
| 531 | } |
| 532 | |
| 533 | /** |
Stefan Bodewig | d7d90da | 2011-07-21 13:18:36 +0000 | [diff] [blame] | 534 | * Gets the uncompressed size of the entry data. |
| 535 | * @return the entry size |
| 536 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 537 | @Override |
Stefan Bodewig | d7d90da | 2011-07-21 13:18:36 +0000 | [diff] [blame] | 538 | public long getSize() { |
| 539 | return size; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Sets the uncompressed size of the entry data. |
| 544 | * @param size the uncompressed size in bytes |
| 545 | * @exception IllegalArgumentException if the specified size is less |
| 546 | * than 0 |
| 547 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 548 | @Override |
Stefan Bodewig | d7d90da | 2011-07-21 13:18:36 +0000 | [diff] [blame] | 549 | public void setSize(long size) { |
| 550 | if (size < 0) { |
| 551 | throw new IllegalArgumentException("invalid entry size"); |
| 552 | } |
| 553 | this.size = size; |
| 554 | } |
| 555 | |
| 556 | /** |
Stefan Bodewig | 15f04cc | 2011-07-25 18:32:19 +0000 | [diff] [blame] | 557 | * Sets the name using the raw bytes and the string created from |
| 558 | * it by guessing or using the configured encoding. |
| 559 | * @param name the name to use created from the raw bytes using |
| 560 | * the guessed or configured encoding |
| 561 | * @param rawName the bytes originally read as name from the |
| 562 | * archive |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 563 | * @since 1.2 |
Stefan Bodewig | 934a96e | 2011-07-23 04:33:44 +0000 | [diff] [blame] | 564 | */ |
Stefan Bodewig | 15f04cc | 2011-07-25 18:32:19 +0000 | [diff] [blame] | 565 | protected void setName(String name, byte[] rawName) { |
Stefan Bodewig | 934a96e | 2011-07-23 04:33:44 +0000 | [diff] [blame] | 566 | setName(name); |
| 567 | this.rawName = rawName; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Returns the raw bytes that made up the name before it has been |
| 572 | * converted using the configured or guessed encoding. |
| 573 | * |
| 574 | * <p>This method will return null if this instance has not been |
| 575 | * read from an archive.</p> |
| 576 | * |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 577 | * @since 1.2 |
Stefan Bodewig | 934a96e | 2011-07-23 04:33:44 +0000 | [diff] [blame] | 578 | */ |
| 579 | public byte[] getRawName() { |
| 580 | if (rawName != null) { |
| 581 | byte[] b = new byte[rawName.length]; |
| 582 | System.arraycopy(rawName, 0, b, 0, rawName.length); |
| 583 | return b; |
| 584 | } |
| 585 | return null; |
| 586 | } |
| 587 | |
| 588 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 589 | * Get the hashCode of the entry. |
| 590 | * This uses the name as the hashcode. |
| 591 | * @return a hashcode. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 592 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 593 | @Override |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 594 | public int hashCode() { |
| 595 | // this method has severe consequences on performance. We cannot rely |
| 596 | // on the super.hashCode() method since super.getName() always return |
| 597 | // the empty string in the current implemention (there's no setter) |
| 598 | // so it is basically draining the performance of a hashmap lookup |
| 599 | return getName().hashCode(); |
| 600 | } |
| 601 | |
| 602 | /** |
Stefan Bodewig | 62cb9c4 | 2010-03-12 16:06:20 +0000 | [diff] [blame] | 603 | * The "general purpose bit" field. |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 604 | * @since 1.1 |
Stefan Bodewig | c7e51ed | 2010-02-19 10:55:27 +0000 | [diff] [blame] | 605 | */ |
Stefan Bodewig | 62cb9c4 | 2010-03-12 16:06:20 +0000 | [diff] [blame] | 606 | public GeneralPurposeBit getGeneralPurposeBit() { |
| 607 | return gpb; |
Stefan Bodewig | c7e51ed | 2010-02-19 10:55:27 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /** |
Stefan Bodewig | 62cb9c4 | 2010-03-12 16:06:20 +0000 | [diff] [blame] | 611 | * The "general purpose bit" field. |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 612 | * @since 1.1 |
Stefan Bodewig | c7e51ed | 2010-02-19 10:55:27 +0000 | [diff] [blame] | 613 | */ |
Stefan Bodewig | 62cb9c4 | 2010-03-12 16:06:20 +0000 | [diff] [blame] | 614 | public void setGeneralPurposeBit(GeneralPurposeBit b) { |
| 615 | gpb = b; |
Stefan Bodewig | c7e51ed | 2010-02-19 10:55:27 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /** |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 619 | * If there are no extra fields, use the given fields as new extra |
| 620 | * data - otherwise merge the fields assuming the existing fields |
| 621 | * and the new fields stem from different locations inside the |
| 622 | * archive. |
| 623 | * @param f the extra fields to merge |
| 624 | * @param local whether the new fields originate from local data |
| 625 | */ |
| 626 | private void mergeExtraFields(ZipExtraField[] f, boolean local) |
| 627 | throws ZipException { |
| 628 | if (extraFields == null) { |
| 629 | setExtraFields(f); |
| 630 | } else { |
Gary D. Gregory | b2a9dd0 | 2012-04-01 13:19:03 +0000 | [diff] [blame] | 631 | for (ZipExtraField element : f) { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 632 | ZipExtraField existing; |
Gary D. Gregory | b2a9dd0 | 2012-04-01 13:19:03 +0000 | [diff] [blame] | 633 | if (element instanceof UnparseableExtraFieldData) { |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 634 | existing = unparseableExtra; |
| 635 | } else { |
Gary D. Gregory | b2a9dd0 | 2012-04-01 13:19:03 +0000 | [diff] [blame] | 636 | existing = getExtraField(element.getHeaderId()); |
Stefan Bodewig | 91695e4 | 2010-02-16 13:59:03 +0000 | [diff] [blame] | 637 | } |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 638 | if (existing == null) { |
Gary D. Gregory | b2a9dd0 | 2012-04-01 13:19:03 +0000 | [diff] [blame] | 639 | addExtraField(element); |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 640 | } else { |
| 641 | if (local) { |
Gary D. Gregory | b2a9dd0 | 2012-04-01 13:19:03 +0000 | [diff] [blame] | 642 | byte[] b = element.getLocalFileDataData(); |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 643 | existing.parseFromLocalFileData(b, 0, b.length); |
| 644 | } else { |
Gary D. Gregory | b2a9dd0 | 2012-04-01 13:19:03 +0000 | [diff] [blame] | 645 | byte[] b = element.getCentralDirectoryData(); |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 646 | existing.parseFromCentralDirectoryData(b, 0, b.length); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | setExtra(); |
| 651 | } |
| 652 | } |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 653 | |
Stefan Bodewig | 17ffd7f | 2009-08-01 14:52:15 +0000 | [diff] [blame] | 654 | public Date getLastModifiedDate() { |
| 655 | return new Date(getTime()); |
| 656 | } |
| 657 | |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 658 | /* (non-Javadoc) |
| 659 | * @see java.lang.Object#equals(java.lang.Object) |
| 660 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 661 | @Override |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 662 | public boolean equals(Object obj) { |
| 663 | if (this == obj) { |
| 664 | return true; |
| 665 | } |
| 666 | if (obj == null || getClass() != obj.getClass()) { |
| 667 | return false; |
| 668 | } |
| 669 | ZipArchiveEntry other = (ZipArchiveEntry) obj; |
Stefan Bodewig | 5066e9a | 2010-01-07 09:34:55 +0000 | [diff] [blame] | 670 | String myName = getName(); |
| 671 | String otherName = other.getName(); |
| 672 | if (myName == null) { |
| 673 | if (otherName != null) { |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 674 | return false; |
| 675 | } |
Stefan Bodewig | 5066e9a | 2010-01-07 09:34:55 +0000 | [diff] [blame] | 676 | } else if (!myName.equals(otherName)) { |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 677 | return false; |
| 678 | } |
Stefan Bodewig | 7be9796 | 2010-01-26 05:24:17 +0000 | [diff] [blame] | 679 | String myComment = getComment(); |
| 680 | String otherComment = other.getComment(); |
| 681 | if (myComment == null) { |
Stefan Bodewig | 4becc32 | 2012-06-03 15:30:31 +0000 | [diff] [blame] | 682 | myComment = ""; |
| 683 | } |
| 684 | if (otherComment == null) { |
| 685 | otherComment = ""; |
Stefan Bodewig | 7be9796 | 2010-01-26 05:24:17 +0000 | [diff] [blame] | 686 | } |
| 687 | return getTime() == other.getTime() |
Stefan Bodewig | 4becc32 | 2012-06-03 15:30:31 +0000 | [diff] [blame] | 688 | && myComment.equals(otherComment) |
Stefan Bodewig | 7be9796 | 2010-01-26 05:24:17 +0000 | [diff] [blame] | 689 | && getInternalAttributes() == other.getInternalAttributes() |
| 690 | && getPlatform() == other.getPlatform() |
| 691 | && getExternalAttributes() == other.getExternalAttributes() |
| 692 | && getMethod() == other.getMethod() |
| 693 | && getSize() == other.getSize() |
| 694 | && getCrc() == other.getCrc() |
| 695 | && getCompressedSize() == other.getCompressedSize() |
| 696 | && Arrays.equals(getCentralDirectoryExtra(), |
| 697 | other.getCentralDirectoryExtra()) |
| 698 | && Arrays.equals(getLocalFileDataExtra(), |
Stefan Bodewig | 62cb9c4 | 2010-03-12 16:06:20 +0000 | [diff] [blame] | 699 | other.getLocalFileDataExtra()) |
| 700 | && gpb.equals(other.gpb); |
Christian Grobmeier | 28c23fb | 2009-04-17 05:14:49 +0000 | [diff] [blame] | 701 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 702 | } |