| 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.io.File; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.InputStream; |
| 24 |
import java.io.RandomAccessFile; |
| 25 |
import java.util.Calendar; |
| 26 |
import java.util.Collections; |
| 27 |
import java.util.Date; |
| 28 |
import java.util.Enumeration; |
| 29 |
import java.util.HashMap; |
| 30 |
import java.util.Map; |
| 31 |
import java.util.logging.Level; |
| 32 |
import java.util.logging.Logger; |
| 33 |
import java.util.zip.CRC32; |
| 34 |
import java.util.zip.Inflater; |
| 35 |
import java.util.zip.InflaterInputStream; |
| 36 |
import java.util.zip.ZipException; |
| 37 |
|
| 38 |
/** |
| 39 |
* Replacement for <code>java.util.ZipFile</code>. |
| 40 |
* |
| 41 |
* <p>This class adds support for file name encodings other than UTF-8 |
| 42 |
* (which is required to work on ZIP files created by native zip tools |
| 43 |
* and is able to skip a preamble like the one found in self |
| 44 |
* extracting archives. Furthermore it returns instances of |
| 45 |
* <code>org.apache.tools.zip.ZipEntry</code> instead of |
| 46 |
* <code>java.util.zip.ZipEntry</code>.</p> |
| 47 |
* |
| 48 |
* <p>It doesn't extend <code>java.util.zip.ZipFile</code> as it would |
| 49 |
* have to reimplement all methods anyway. Like |
| 50 |
* <code>java.util.ZipFile</code>, it uses RandomAccessFile under the |
| 51 |
* covers and supports compressed and uncompressed entries.</p> |
| 52 |
* |
| 53 |
* <p>The method signatures mimic the ones of |
| 54 |
* <code>java.util.zip.ZipFile</code>, with a couple of exceptions: |
| 55 |
* |
| 56 |
* <ul> |
| 57 |
* <li>There is no getName method.</li> |
| 58 |
* <li>entries has been renamed to getEntries.</li> |
| 59 |
* <li>getEntries and getEntry return |
| 60 |
* <code>org.apache.tools.zip.ZipEntry</code> instances.</li> |
| 61 |
* <li>close is allowed to throw IOException.</li> |
| 62 |
* </ul> |
| 63 |
* |
| 64 |
*/ |
| 65 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
| 66 |
public class ZipFile { |
| 67 |
|
| 68 |
private static final Logger logger = Logger.getLogger(ZipFile.class.getName()); |
| 69 |
|
| 70 |
private static final int HASH_SIZE = 509; |
| 71 |
private static final int SHORT = 2; |
| 72 |
private static final int WORD = 4; |
| 73 |
private static final int NIBLET_MASK = 0x0f; |
| 74 |
private static final int BYTE_SHIFT = 8; |
| 75 |
private static final int POS_0 = 0; |
| 76 |
private static final int POS_1 = 1; |
| 77 |
private static final int POS_2 = 2; |
| 78 |
private static final int POS_3 = 3; |
| 79 |
|
| 80 |
/** |
| 81 |
* Maps ZipEntrys to Longs, recording the offsets of the local |
| 82 |
* file headers. |
| 83 |
*/ |
| 84 |
private final Map entries = new HashMap(HASH_SIZE); |
| 85 |
|
| 86 |
/** |
| 87 |
* Maps String to ZipEntrys, name -> actual entry. |
| 88 |
*/ |
| 89 |
private final Map nameMap = new HashMap(HASH_SIZE); |
| 90 |
|
| 91 |
private static final class OffsetEntry { |
| 92 |
private long headerOffset = -1; |
| 93 |
private long dataOffset = -1; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* The encoding to use for filenames and the file comment. |
| 98 |
* |
| 99 |
* <p>For a list of possible values see <a |
| 100 |
* href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html</a>. |
| 101 |
* Defaults to the platform's default character encoding.</p> |
| 102 |
*/ |
| 103 |
private String encoding = null; |
| 104 |
|
| 105 |
/** |
| 106 |
* The zip encoding to use for filenames and the file comment. |
| 107 |
*/ |
| 108 |
private final ZipEncoding zipEncoding; |
| 109 |
|
| 110 |
/** |
| 111 |
* The actual data source. |
| 112 |
*/ |
| 113 |
private RandomAccessFile archive; |
| 114 |
|
| 115 |
/** |
| 116 |
* Whether to look for and use Unicode extra fields. |
| 117 |
*/ |
| 118 |
private final boolean useUnicodeExtraFields; |
| 119 |
|
| 120 |
/** |
| 121 |
* Opens the given file for reading, assuming the platform's |
| 122 |
* native encoding for file names. |
| 123 |
* |
| 124 |
* @param f the archive. |
| 125 |
* |
| 126 |
* @throws IOException if an error occurs while reading the file. |
| 127 |
*/ |
| 128 |
public ZipFile(File f) throws IOException { |
| 129 |
this(f, null); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Opens the given file for reading, assuming the platform's |
| 134 |
* native encoding for file names. |
| 135 |
* |
| 136 |
* @param name name of the archive. |
| 137 |
* |
| 138 |
* @throws IOException if an error occurs while reading the file. |
| 139 |
*/ |
| 140 |
public ZipFile(String name) throws IOException { |
| 141 |
this(new File(name), null); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Opens the given file for reading, assuming the specified |
| 146 |
* encoding for file names, scanning unicode extra fields. |
| 147 |
* |
| 148 |
* @param name name of the archive. |
| 149 |
* @param encoding the encoding to use for file names |
| 150 |
* |
| 151 |
* @throws IOException if an error occurs while reading the file. |
| 152 |
*/ |
| 153 |
public ZipFile(String name, String encoding) throws IOException { |
| 154 |
this(new File(name), encoding, true); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Opens the given file for reading, assuming the specified |
| 159 |
* encoding for file names and scanning for unicode extra fields. |
| 160 |
* |
| 161 |
* @param f the archive. |
| 162 |
* @param encoding the encoding to use for file names, use null |
| 163 |
* for the platform's default encoding |
| 164 |
* |
| 165 |
* @throws IOException if an error occurs while reading the file. |
| 166 |
*/ |
| 167 |
public ZipFile(File f, String encoding) throws IOException { |
| 168 |
this(f, encoding, true); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Opens the given file for reading, assuming the specified |
| 173 |
* encoding for file names. |
| 174 |
* |
| 175 |
* @param f the archive. |
| 176 |
* @param encoding the encoding to use for file names, use null |
| 177 |
* for the platform's default encoding |
| 178 |
* @param useUnicodeExtraFields whether to use InfoZIP Unicode |
| 179 |
* Extra Fields (if present) to set the file names. |
| 180 |
* |
| 181 |
* @throws IOException if an error occurs while reading the file. |
| 182 |
*/ |
| 183 |
public ZipFile(File f, String encoding, boolean useUnicodeExtraFields) |
| 184 |
throws IOException { |
| 185 |
this.encoding = encoding; |
| 186 |
this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); |
| 187 |
this.useUnicodeExtraFields = useUnicodeExtraFields; |
| 188 |
archive = new RandomAccessFile(f, "r"); |
| 189 |
boolean success = false; |
| 190 |
try { |
| 191 |
Map entriesWithoutUTF8Flag = populateFromCentralDirectory(); |
| 192 |
resolveLocalFileHeaderData(entriesWithoutUTF8Flag); |
| 193 |
success = true; |
| 194 |
} finally { |
| 195 |
if (!success) { |
| 196 |
try { |
| 197 |
archive.close(); |
| 198 |
} catch (IOException e2) { |
| 199 |
// swallow, throw the original exception instead |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* The encoding to use for filenames and the file comment. |
| 207 |
* |
| 208 |
* @return null if using the platform's default character encoding. |
| 209 |
*/ |
| 210 |
public String getEncoding() { |
| 211 |
return encoding; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Closes the archive. |
| 216 |
* @throws IOException if an error occurs closing the archive. |
| 217 |
*/ |
| 218 |
public void close() throws IOException { |
| 219 |
archive.close(); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* close a zipfile quietly; throw no io fault, do nothing |
| 224 |
* on a null parameter |
| 225 |
* @param zipfile file to close, can be null |
| 226 |
*/ |
| 227 |
public static void closeQuietly(ZipFile zipfile) { |
| 228 |
if (zipfile != null) { |
| 229 |
try { |
| 230 |
zipfile.close(); |
| 231 |
} catch (IOException e) { |
| 232 |
//ignore |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Returns all entries. |
| 239 |
* @return all entries as {@link ZipEntry} instances |
| 240 |
*/ |
| 241 |
public Enumeration getEntries() { |
| 242 |
return Collections.enumeration(entries.keySet()); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Returns a named entry - or <code>null</code> if no entry by |
| 247 |
* that name exists. |
| 248 |
* @param name name of the entry. |
| 249 |
* @return the ZipEntry corresponding to the given name - or |
| 250 |
* <code>null</code> if not present. |
| 251 |
*/ |
| 252 |
public ZipEntry getEntry(String name) { |
| 253 |
return (ZipEntry) nameMap.get(name); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Returns an InputStream for reading the contents of the given entry. |
| 258 |
* @param ze the entry to get the stream for. |
| 259 |
* @return a stream to read the entry from. |
| 260 |
* @throws IOException if unable to create an input stream from the zipenty |
| 261 |
* @throws ZipException if the zipentry has an unsupported |
| 262 |
* compression method |
| 263 |
*/ |
| 264 |
public InputStream getInputStream(ZipEntry ze) |
| 265 |
throws IOException, ZipException { |
| 266 |
OffsetEntry offsetEntry = (OffsetEntry) entries.get(ze); |
| 267 |
if (offsetEntry == null) { |
| 268 |
return null; |
| 269 |
} |
| 270 |
long start = offsetEntry.dataOffset; |
| 271 |
BoundedInputStream bis = |
| 272 |
new BoundedInputStream(start, ze.getCompressedSize()); |
| 273 |
switch (ze.getMethod()) { |
| 274 |
case ZipEntry.STORED: |
| 275 |
return bis; |
| 276 |
case ZipEntry.DEFLATED: |
| 277 |
bis.addDummy(); |
| 278 |
return new InflaterInputStream(bis, new Inflater(true)); |
| 279 |
default: |
| 280 |
throw new ZipException("Found unsupported compression method " |
| 281 |
+ ze.getMethod()); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
private static final int CFH_LEN = |
| 286 |
/* version made by */ SHORT |
| 287 |
/* version needed to extract */ + SHORT |
| 288 |
/* general purpose bit flag */ + SHORT |
| 289 |
/* compression method */ + SHORT |
| 290 |
/* last mod file time */ + SHORT |
| 291 |
/* last mod file date */ + SHORT |
| 292 |
/* crc-32 */ + WORD |
| 293 |
/* compressed size */ + WORD |
| 294 |
/* uncompressed size */ + WORD |
| 295 |
/* filename length */ + SHORT |
| 296 |
/* extra field length */ + SHORT |
| 297 |
/* file comment length */ + SHORT |
| 298 |
/* disk number start */ + SHORT |
| 299 |
/* internal file attributes */ + SHORT |
| 300 |
/* external file attributes */ + WORD |
| 301 |
/* relative offset of local header */ + WORD; |
| 302 |
|
| 303 |
/** |
| 304 |
* Reads the central directory of the given archive and populates |
| 305 |
* the internal tables with ZipEntry instances. |
| 306 |
* |
| 307 |
* <p>The ZipEntrys will know all data that can be obtained from |
| 308 |
* the central directory alone, but not the data that requires the |
| 309 |
* local file header or additional data to be read.</p> |
| 310 |
* |
| 311 |
* @return a Map<ZipEntry, NameAndComment>> of |
| 312 |
* zipentries that didn't have the language encoding flag set when |
| 313 |
* read. |
| 314 |
*/ |
| 315 |
private Map populateFromCentralDirectory() |
| 316 |
throws IOException { |
| 317 |
HashMap noUTF8Flag = new HashMap(); |
| 318 |
|
| 319 |
positionAtCentralDirectory(); |
| 320 |
|
| 321 |
byte[] cfh = new byte[CFH_LEN]; |
| 322 |
|
| 323 |
byte[] signatureBytes = new byte[WORD]; |
| 324 |
archive.readFully(signatureBytes); |
| 325 |
long sig = ZipLong.getValue(signatureBytes); |
| 326 |
final long cfhSig = ZipLong.getValue(ZipOutputStream.CFH_SIG); |
| 327 |
if (sig != cfhSig && startsWithLocalFileHeader()) { |
| 328 |
throw new IOException("central directory is empty, can't expand" |
| 329 |
+ " corrupt archive."); |
| 330 |
} |
| 331 |
while (sig == cfhSig) { |
| 332 |
archive.readFully(cfh); |
| 333 |
int off = 0; |
| 334 |
ZipEntry ze = new ZipEntry(); |
| 335 |
|
| 336 |
int versionMadeBy = ZipShort.getValue(cfh, off); |
| 337 |
off += SHORT; |
| 338 |
ze.setPlatform((versionMadeBy >> BYTE_SHIFT) & NIBLET_MASK); |
| 339 |
|
| 340 |
off += SHORT; // skip version info |
| 341 |
|
| 342 |
final int generalPurposeFlag = ZipShort.getValue(cfh, off); |
| 343 |
final boolean hasUTF8Flag = |
| 344 |
(generalPurposeFlag & ZipOutputStream.UFT8_NAMES_FLAG) != 0; |
| 345 |
final ZipEncoding entryEncoding = |
| 346 |
hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding; |
| 347 |
|
| 348 |
off += SHORT; |
| 349 |
|
| 350 |
ze.setMethod(ZipShort.getValue(cfh, off)); |
| 351 |
off += SHORT; |
| 352 |
|
| 353 |
// FIXME this is actually not very cpu cycles friendly as we are converting from |
| 354 |
// dos to java while the underlying Sun implementation will convert |
| 355 |
// from java to dos time for internal storage... |
| 356 |
long time = dosToJavaTime(ZipLong.getValue(cfh, off)); |
| 357 |
ze.setTime(time); |
| 358 |
off += WORD; |
| 359 |
|
| 360 |
ze.setCrc(ZipLong.getValue(cfh, off)); |
| 361 |
off += WORD; |
| 362 |
|
| 363 |
ze.setCompressedSize(ZipLong.getValue(cfh, off)); |
| 364 |
off += WORD; |
| 365 |
|
| 366 |
ze.setSize(ZipLong.getValue(cfh, off)); |
| 367 |
off += WORD; |
| 368 |
|
| 369 |
int fileNameLen = ZipShort.getValue(cfh, off); |
| 370 |
off += SHORT; |
| 371 |
|
| 372 |
int extraLen = ZipShort.getValue(cfh, off); |
| 373 |
off += SHORT; |
| 374 |
|
| 375 |
int commentLen = ZipShort.getValue(cfh, off); |
| 376 |
off += SHORT; |
| 377 |
|
| 378 |
off += SHORT; // disk number |
| 379 |
|
| 380 |
ze.setInternalAttributes(ZipShort.getValue(cfh, off)); |
| 381 |
off += SHORT; |
| 382 |
|
| 383 |
ze.setExternalAttributes(ZipLong.getValue(cfh, off)); |
| 384 |
off += WORD; |
| 385 |
|
| 386 |
byte[] fileName = new byte[fileNameLen]; |
| 387 |
archive.readFully(fileName); |
| 388 |
ze.setName(entryEncoding.decode(fileName)); |
| 389 |
|
| 390 |
// LFH offset, |
| 391 |
OffsetEntry offset = new OffsetEntry(); |
| 392 |
offset.headerOffset = ZipLong.getValue(cfh, off); |
| 393 |
// data offset will be filled later |
| 394 |
entries.put(ze, offset); |
| 395 |
|
| 396 |
nameMap.put(ze.getName(), ze); |
| 397 |
|
| 398 |
byte[] cdExtraData = new byte[extraLen]; |
| 399 |
archive.readFully(cdExtraData); |
| 400 |
ze.setCentralDirectoryExtra(cdExtraData); |
| 401 |
|
| 402 |
byte[] comment = new byte[commentLen]; |
| 403 |
archive.readFully(comment); |
| 404 |
ze.setComment(entryEncoding.decode(comment)); |
| 405 |
|
| 406 |
archive.readFully(signatureBytes); |
| 407 |
sig = ZipLong.getValue(signatureBytes); |
| 408 |
|
| 409 |
if (!hasUTF8Flag && useUnicodeExtraFields) { |
| 410 |
noUTF8Flag.put(ze, new NameAndComment(fileName, comment)); |
| 411 |
} |
| 412 |
} |
| 413 |
return noUTF8Flag; |
| 414 |
} |
| 415 |
|
| 416 |
private static final int MIN_EOCD_SIZE = |
| 417 |
/* end of central dir signature */ WORD |
| 418 |
/* number of this disk */ + SHORT |
| 419 |
/* number of the disk with the */ |
| 420 |
/* start of the central directory */ + SHORT |
| 421 |
/* total number of entries in */ |
| 422 |
/* the central dir on this disk */ + SHORT |
| 423 |
/* total number of entries in */ |
| 424 |
/* the central dir */ + SHORT |
| 425 |
/* size of the central directory */ + WORD |
| 426 |
/* offset of start of central */ |
| 427 |
/* directory with respect to */ |
| 428 |
/* the starting disk number */ + WORD |
| 429 |
/* zipfile comment length */ + SHORT; |
| 430 |
|
| 431 |
private static final int MAX_EOCD_SIZE = MIN_EOCD_SIZE |
| 432 |
/* maximum length of zipfile comment */ + 0xFFFF; |
| 433 |
|
| 434 |
private static final int CFD_LOCATOR_OFFSET = |
| 435 |
/* end of central dir signature */ WORD |
| 436 |
/* number of this disk */ + SHORT |
| 437 |
/* number of the disk with the */ |
| 438 |
/* start of the central directory */ + SHORT |
| 439 |
/* total number of entries in */ |
| 440 |
/* the central dir on this disk */ + SHORT |
| 441 |
/* total number of entries in */ |
| 442 |
/* the central dir */ + SHORT |
| 443 |
/* size of the central directory */ + WORD; |
| 444 |
|
| 445 |
/** |
| 446 |
* Searches for the "End of central dir record", parses |
| 447 |
* it and positions the stream at the first central directory |
| 448 |
* record. |
| 449 |
*/ |
| 450 |
private void positionAtCentralDirectory() |
| 451 |
throws IOException { |
| 452 |
boolean found = false; |
| 453 |
long off = archive.length() - MIN_EOCD_SIZE; |
| 454 |
long stopSearching = Math.max(0L, archive.length() - MAX_EOCD_SIZE); |
| 455 |
if (off >= 0) { |
| 456 |
archive.seek(off); |
| 457 |
byte[] sig = ZipOutputStream.EOCD_SIG; |
| 458 |
int curr = archive.read(); |
| 459 |
while (off >= stopSearching && curr != -1) { |
| 460 |
if (curr == sig[POS_0]) { |
| 461 |
curr = archive.read(); |
| 462 |
if (curr == sig[POS_1]) { |
| 463 |
curr = archive.read(); |
| 464 |
if (curr == sig[POS_2]) { |
| 465 |
curr = archive.read(); |
| 466 |
if (curr == sig[POS_3]) { |
| 467 |
found = true; |
| 468 |
break; |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
archive.seek(--off); |
| 474 |
curr = archive.read(); |
| 475 |
} |
| 476 |
} |
| 477 |
if (!found) { |
| 478 |
throw new ZipException("archive is not a ZIP archive"); |
| 479 |
} |
| 480 |
archive.seek(off + CFD_LOCATOR_OFFSET); |
| 481 |
byte[] cfdOffset = new byte[WORD]; |
| 482 |
archive.readFully(cfdOffset); |
| 483 |
archive.seek(ZipLong.getValue(cfdOffset)); |
| 484 |
} |
| 485 |
|
| 486 |
/** |
| 487 |
* Number of bytes in local file header up to the "length of |
| 488 |
* filename" entry. |
| 489 |
*/ |
| 490 |
private static final long LFH_OFFSET_FOR_FILENAME_LENGTH = |
| 491 |
/* local file header signature */ WORD |
| 492 |
/* version needed to extract */ + SHORT |
| 493 |
/* general purpose bit flag */ + SHORT |
| 494 |
/* compression method */ + SHORT |
| 495 |
/* last mod file time */ + SHORT |
| 496 |
/* last mod file date */ + SHORT |
| 497 |
/* crc-32 */ + WORD |
| 498 |
/* compressed size */ + WORD |
| 499 |
/* uncompressed size */ + WORD; |
| 500 |
|
| 501 |
/** |
| 502 |
* Walks through all recorded entries and adds the data available |
| 503 |
* from the local file header. |
| 504 |
* |
| 505 |
* <p>Also records the offsets for the data to read from the |
| 506 |
* entries.</p> |
| 507 |
*/ |
| 508 |
private void resolveLocalFileHeaderData(Map entriesWithoutUTF8Flag) |
| 509 |
throws IOException { |
| 510 |
Enumeration e = getEntries(); |
| 511 |
while (e.hasMoreElements()) { |
| 512 |
ZipEntry ze = (ZipEntry) e.nextElement(); |
| 513 |
OffsetEntry offsetEntry = (OffsetEntry) entries.get(ze); |
| 514 |
long offset = offsetEntry.headerOffset; |
| 515 |
archive.seek(offset + LFH_OFFSET_FOR_FILENAME_LENGTH); |
| 516 |
byte[] b = new byte[SHORT]; |
| 517 |
archive.readFully(b); |
| 518 |
int fileNameLen = ZipShort.getValue(b); |
| 519 |
archive.readFully(b); |
| 520 |
int extraFieldLen = ZipShort.getValue(b); |
| 521 |
int lenToSkip = fileNameLen; |
| 522 |
while (lenToSkip > 0) { |
| 523 |
int skipped = archive.skipBytes(lenToSkip); |
| 524 |
if (skipped <= 0) { |
| 525 |
throw new RuntimeException("failed to skip file name in" |
| 526 |
+ " local file header"); |
| 527 |
} |
| 528 |
lenToSkip -= skipped; |
| 529 |
} |
| 530 |
byte[] localExtraData = new byte[extraFieldLen]; |
| 531 |
archive.readFully(localExtraData); |
| 532 |
ze.setExtra(localExtraData); |
| 533 |
/*dataOffsets.put(ze, |
| 534 |
new Long(offset + LFH_OFFSET_FOR_FILENAME_LENGTH |
| 535 |
+ SHORT + SHORT + fileNameLen + extraFieldLen)); |
| 536 |
*/ |
| 537 |
offsetEntry.dataOffset = offset + LFH_OFFSET_FOR_FILENAME_LENGTH |
| 538 |
+ SHORT + SHORT + fileNameLen + extraFieldLen; |
| 539 |
|
| 540 |
if (entriesWithoutUTF8Flag.containsKey(ze)) { |
| 541 |
setNameAndCommentFromExtraFields(ze, |
| 542 |
(NameAndComment) |
| 543 |
entriesWithoutUTF8Flag.get(ze)); |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* Convert a DOS date/time field to a Date object. |
| 550 |
* |
| 551 |
* @param zipDosTime contains the stored DOS time. |
| 552 |
* @return a Date instance corresponding to the given time. |
| 553 |
*/ |
| 554 |
protected static Date fromDosTime(ZipLong zipDosTime) { |
| 555 |
long dosTime = zipDosTime.getValue(); |
| 556 |
return new Date(dosToJavaTime(dosTime)); |
| 557 |
} |
| 558 |
|
| 559 |
/* |
| 560 |
* Converts DOS time to Java time (number of milliseconds since epoch). |
| 561 |
*/ |
| 562 |
private static long dosToJavaTime(long dosTime) { |
| 563 |
Calendar cal = Calendar.getInstance(); |
| 564 |
// CheckStyle:MagicNumberCheck OFF - no point |
| 565 |
cal.set(Calendar.YEAR, (int) ((dosTime >> 25) & 0x7f) + 1980); |
| 566 |
cal.set(Calendar.MONTH, (int) ((dosTime >> 21) & 0x0f) - 1); |
| 567 |
cal.set(Calendar.DATE, (int) (dosTime >> 16) & 0x1f); |
| 568 |
cal.set(Calendar.HOUR_OF_DAY, (int) (dosTime >> 11) & 0x1f); |
| 569 |
cal.set(Calendar.MINUTE, (int) (dosTime >> 5) & 0x3f); |
| 570 |
cal.set(Calendar.SECOND, (int) (dosTime << 1) & 0x3e); |
| 571 |
// CheckStyle:MagicNumberCheck ON |
| 572 |
return cal.getTime().getTime(); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
/** |
| 577 |
* Retrieve a String from the given bytes using the encoding set |
| 578 |
* for this ZipFile. |
| 579 |
* |
| 580 |
* @param bytes the byte array to transform |
| 581 |
* @return String obtained by using the given encoding |
| 582 |
* @throws ZipException if the encoding cannot be recognized. |
| 583 |
*/ |
| 584 |
protected String getString(byte[] bytes) throws ZipException { |
| 585 |
try { |
| 586 |
return ZipEncodingHelper.getZipEncoding(encoding).decode(bytes); |
| 587 |
} catch (IOException ex) { |
| 588 |
throw new ZipException("Failed to decode name: " + ex.getMessage()); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Checks whether the archive starts with a LFH. If it doesn't, |
| 594 |
* it may be an empty archive. |
| 595 |
*/ |
| 596 |
private boolean startsWithLocalFileHeader() throws IOException { |
| 597 |
archive.seek(0); |
| 598 |
final byte[] start = new byte[WORD]; |
| 599 |
archive.readFully(start); |
| 600 |
for (int i = 0; i < start.length; i++) { |
| 601 |
if (start[i] != ZipOutputStream.LFH_SIG[i]) { |
| 602 |
return false; |
| 603 |
} |
| 604 |
} |
| 605 |
return true; |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* If the entry has Unicode*ExtraFields and the CRCs of the |
| 610 |
* names/comments match those of the extra fields, transfer the |
| 611 |
* known Unicode values from the extra field. |
| 612 |
*/ |
| 613 |
private void setNameAndCommentFromExtraFields(ZipEntry ze, |
| 614 |
NameAndComment nc) { |
| 615 |
UnicodePathExtraField name = (UnicodePathExtraField) |
| 616 |
ze.getExtraField(UnicodePathExtraField.UPATH_ID); |
| 617 |
String originalName = ze.getName(); |
| 618 |
String newName = getUnicodeStringIfOriginalMatches(name, nc.name); |
| 619 |
if (newName != null && !originalName.equals(newName)) { |
| 620 |
ze.setName(newName); |
| 621 |
nameMap.remove(originalName); |
| 622 |
nameMap.put(newName, ze); |
| 623 |
} |
| 624 |
|
| 625 |
if (nc.comment != null && nc.comment.length > 0) { |
| 626 |
UnicodeCommentExtraField cmt = (UnicodeCommentExtraField) |
| 627 |
ze.getExtraField(UnicodeCommentExtraField.UCOM_ID); |
| 628 |
String newComment = |
| 629 |
getUnicodeStringIfOriginalMatches(cmt, nc.comment); |
| 630 |
if (newComment != null) { |
| 631 |
ze.setComment(newComment); |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* If the stored CRC matches the one of the given name, return the |
| 638 |
* Unicode name of the given field. |
| 639 |
* |
| 640 |
* <p>If the field is null or the CRCs don't match, return null |
| 641 |
* instead.</p> |
| 642 |
*/ |
| 643 |
private String getUnicodeStringIfOriginalMatches(AbstractUnicodeExtraField f, |
| 644 |
byte[] orig) { |
| 645 |
if (f != null) { |
| 646 |
CRC32 crc32 = new CRC32(); |
| 647 |
crc32.update(orig); |
| 648 |
long origCRC32 = crc32.getValue(); |
| 649 |
|
| 650 |
if (origCRC32 == f.getNameCRC32()) { |
| 651 |
try { |
| 652 |
return ZipEncodingHelper |
| 653 |
.UTF8_ZIP_ENCODING.decode(f.getUnicodeName()); |
| 654 |
} catch (IOException ex) { |
| 655 |
// UTF-8 unsupported? should be impossible the |
| 656 |
// Unicode*ExtraField must contain some bad bytes |
| 657 |
|
| 658 |
logger.log(Level.WARNING, "ZipFile: UTF-8 unsupported." |
| 659 |
+ " should be impossible the Unicode*ExtraField must contain some bad bytes.", ex); |
| 660 |
return null; |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
return null; |
| 665 |
} |
| 666 |
|
| 667 |
/** |
| 668 |
* InputStream that delegates requests to the underlying |
| 669 |
* RandomAccessFile, making sure that only bytes from a certain |
| 670 |
* range can be read. |
| 671 |
*/ |
| 672 |
private class BoundedInputStream extends InputStream { |
| 673 |
private long remaining; |
| 674 |
private long loc; |
| 675 |
private boolean addDummyByte = false; |
| 676 |
|
| 677 |
BoundedInputStream(long start, long remaining) { |
| 678 |
this.remaining = remaining; |
| 679 |
loc = start; |
| 680 |
} |
| 681 |
|
| 682 |
public int read() throws IOException { |
| 683 |
if (remaining-- <= 0) { |
| 684 |
if (addDummyByte) { |
| 685 |
addDummyByte = false; |
| 686 |
return 0; |
| 687 |
} |
| 688 |
return -1; |
| 689 |
} |
| 690 |
synchronized (archive) { |
| 691 |
archive.seek(loc++); |
| 692 |
return archive.read(); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
public int read(byte[] b, int off, int len) throws IOException { |
| 697 |
if (remaining <= 0) { |
| 698 |
if (addDummyByte) { |
| 699 |
addDummyByte = false; |
| 700 |
b[off] = 0; |
| 701 |
return 1; |
| 702 |
} |
| 703 |
return -1; |
| 704 |
} |
| 705 |
|
| 706 |
if (len <= 0) { |
| 707 |
return 0; |
| 708 |
} |
| 709 |
|
| 710 |
if (len > remaining) { |
| 711 |
len = (int) remaining; |
| 712 |
} |
| 713 |
int ret = -1; |
| 714 |
synchronized (archive) { |
| 715 |
archive.seek(loc); |
| 716 |
ret = archive.read(b, off, len); |
| 717 |
} |
| 718 |
if (ret > 0) { |
| 719 |
loc += ret; |
| 720 |
remaining -= ret; |
| 721 |
} |
| 722 |
return ret; |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* Inflater needs an extra dummy byte for nowrap - see |
| 727 |
* Inflater's javadocs. |
| 728 |
*/ |
| 729 |
void addDummy() { |
| 730 |
addDummyByte = true; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
private static final class NameAndComment { |
| 735 |
private final byte[] name; |
| 736 |
private final byte[] comment; |
| 737 |
private NameAndComment(byte[] name, byte[] comment) { |
| 738 |
this.name = name; |
| 739 |
this.comment = comment; |
| 740 |
} |
| 741 |
} |
| 742 |
} |