src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java

Fri, 21 Dec 2012 15:27:55 +0000

author
vromero
date
Fri, 21 Dec 2012 15:27:55 +0000
changeset 1467
189b26e3818f
parent 1442
fcf89720ae71
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8003512: javac doesn't work with jar files with >64k entries
Reviewed-by: jjg, ksrini
Contributed-by: martinrb@google.com

jjg@36 1 /*
jjh@1305 2 * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@36 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@36 4 *
jjg@36 5 * This code is free software; you can redistribute it and/or modify it
jjg@36 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@36 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@36 10 *
jjg@36 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@36 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@36 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@36 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@36 15 * accompanied this code).
jjg@36 16 *
jjg@36 17 * You should have received a copy of the GNU General Public License version
jjg@36 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@36 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@36 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@36 24 */
jjg@36 25
jjg@50 26 package com.sun.tools.javac.file;
duke@1 27
jjg@103 28
jjg@50 29 import java.io.File;
jjg@50 30 import java.io.FileNotFoundException;
jjg@50 31 import java.io.IOException;
jjg@50 32 import java.io.RandomAccessFile;
jjg@424 33 import java.lang.ref.Reference;
jjg@103 34 import java.lang.ref.SoftReference;
jjg@50 35 import java.util.ArrayList;
jjg@50 36 import java.util.Arrays;
jjg@71 37 import java.util.Calendar;
jjg@50 38 import java.util.Collections;
mcimadamore@1279 39 import java.util.LinkedHashMap;
jjg@50 40 import java.util.HashMap;
duke@1 41 import java.util.List;
jjg@50 42 import java.util.Map;
jjg@50 43 import java.util.Set;
jjg@50 44 import java.util.zip.DataFormatException;
jjg@50 45 import java.util.zip.Inflater;
jjg@50 46 import java.util.zip.ZipException;
duke@1 47
jjg@103 48 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
jjg@103 49 import com.sun.tools.javac.file.RelativePath.RelativeFile;
jjg@103 50
jjg@839 51 /**
jjg@839 52 * This class implements the building of index of a zip archive and access to
jjg@839 53 * its context. It also uses a prebuilt index if available.
jjg@839 54 * It supports invocations where it will serialize an optimized zip index file
jjg@839 55 * to disk.
duke@1 56 *
jjg@839 57 * In order to use a secondary index file, set "usezipindex" in the Options
jjg@839 58 * object when JavacFileManager is invoked. (You can pass "-XDusezipindex" on
jjg@839 59 * the command line.)
duke@1 60 *
jjg@839 61 * Location where to look for/generate optimized zip index files can be
jjg@1358 62 * provided using "{@code -XDcachezipindexdir=<directory>}". If this flag is not
jjg@839 63 * provided, the default location is the value of the "java.io.tmpdir" system
jjg@839 64 * property.
duke@1 65 *
jjg@839 66 * If "-XDwritezipindexfiles" is specified, there will be new optimized index
jjg@839 67 * file created for each archive, used by the compiler for compilation, at the
jjg@839 68 * location specified by the "cachezipindexdir" option.
duke@1 69 *
jjg@839 70 * If system property nonBatchMode option is specified the compiler will use
jjg@839 71 * timestamp checking to reindex the zip files if it is needed. In batch mode
jjg@839 72 * the timestamps are not checked and the compiler uses the cached indexes.
jjg@333 73 *
jjg@581 74 * <p><b>This is NOT part of any supported API.
jjg@333 75 * If you write code that depends on this, you do so at your own risk.
jjg@333 76 * This code and its internal interfaces are subject to change or
jjg@333 77 * deletion without notice.</b>
duke@1 78 */
duke@1 79 public class ZipFileIndex {
duke@1 80 private static final String MIN_CHAR = String.valueOf(Character.MIN_VALUE);
duke@1 81 private static final String MAX_CHAR = String.valueOf(Character.MAX_VALUE);
duke@1 82
duke@1 83 public final static long NOT_MODIFIED = Long.MIN_VALUE;
duke@1 84
duke@1 85
vromero@1442 86 private static final boolean NON_BATCH_MODE = System.getProperty("nonBatchMode") != null;// TODO: Use -XD compiler switch for this.
duke@1 87
jjg@839 88 private Map<RelativeDirectory, DirectoryEntry> directories =
jjg@839 89 Collections.<RelativeDirectory, DirectoryEntry>emptyMap();
jjg@839 90 private Set<RelativeDirectory> allDirs =
jjg@839 91 Collections.<RelativeDirectory>emptySet();
duke@1 92
duke@1 93 // ZipFileIndex data entries
jjg@839 94 final File zipFile;
jjg@424 95 private Reference<File> absFileRef;
jjg@839 96 long zipFileLastModified = NOT_MODIFIED;
duke@1 97 private RandomAccessFile zipRandomFile;
jjg@57 98 private Entry[] entries;
duke@1 99
duke@1 100 private boolean readFromIndex = false;
duke@1 101 private File zipIndexFile = null;
duke@1 102 private boolean triedToReadIndex = false;
jjg@103 103 final RelativeDirectory symbolFilePrefix;
jjg@839 104 private final int symbolFilePrefixLength;
duke@1 105 private boolean hasPopulatedData = false;
jjg@839 106 long lastReferenceTimeStamp = NOT_MODIFIED;
duke@1 107
jjg@839 108 private final boolean usePreindexedCache;
jjg@839 109 private final String preindexedCacheLocation;
duke@1 110
duke@1 111 private boolean writeIndex = false;
duke@1 112
jjg@839 113 private Map<String, SoftReference<RelativeDirectory>> relativeDirectoryCache =
jjg@103 114 new HashMap<String, SoftReference<RelativeDirectory>>();
jjg@103 115
jjg@839 116
jjg@839 117 public synchronized boolean isOpen() {
jjg@839 118 return (zipRandomFile != null);
duke@1 119 }
duke@1 120
jjg@839 121 ZipFileIndex(File zipFile, RelativeDirectory symbolFilePrefix, boolean writeIndex,
duke@1 122 boolean useCache, String cacheLocation) throws IOException {
duke@1 123 this.zipFile = zipFile;
jjg@57 124 this.symbolFilePrefix = symbolFilePrefix;
jjg@57 125 this.symbolFilePrefixLength = (symbolFilePrefix == null ? 0 :
jjg@103 126 symbolFilePrefix.getPath().getBytes("UTF-8").length);
duke@1 127 this.writeIndex = writeIndex;
duke@1 128 this.usePreindexedCache = useCache;
duke@1 129 this.preindexedCacheLocation = cacheLocation;
duke@1 130
duke@1 131 if (zipFile != null) {
duke@1 132 this.zipFileLastModified = zipFile.lastModified();
duke@1 133 }
duke@1 134
duke@1 135 // Validate integrity of the zip file
duke@1 136 checkIndex();
duke@1 137 }
duke@1 138
jjg@839 139 @Override
duke@1 140 public String toString() {
jjg@103 141 return "ZipFileIndex[" + zipFile + "]";
duke@1 142 }
duke@1 143
duke@1 144 // Just in case...
jjg@839 145 @Override
jjg@839 146 protected void finalize() throws Throwable {
duke@1 147 closeFile();
jjg@839 148 super.finalize();
duke@1 149 }
duke@1 150
duke@1 151 private boolean isUpToDate() {
jjg@839 152 if (zipFile != null
jjg@839 153 && ((!NON_BATCH_MODE) || zipFileLastModified == zipFile.lastModified())
jjg@839 154 && hasPopulatedData) {
duke@1 155 return true;
duke@1 156 }
duke@1 157
duke@1 158 return false;
duke@1 159 }
duke@1 160
duke@1 161 /**
duke@1 162 * Here we need to make sure that the ZipFileIndex is valid. Check the timestamp of the file and
duke@1 163 * if its the same as the one at the time the index was build we don't need to reopen anything.
duke@1 164 */
duke@1 165 private void checkIndex() throws IOException {
duke@1 166 boolean isUpToDate = true;
duke@1 167 if (!isUpToDate()) {
duke@1 168 closeFile();
duke@1 169 isUpToDate = false;
duke@1 170 }
duke@1 171
duke@1 172 if (zipRandomFile != null || isUpToDate) {
duke@1 173 lastReferenceTimeStamp = System.currentTimeMillis();
duke@1 174 return;
duke@1 175 }
duke@1 176
duke@1 177 hasPopulatedData = true;
duke@1 178
duke@1 179 if (readIndex()) {
duke@1 180 lastReferenceTimeStamp = System.currentTimeMillis();
duke@1 181 return;
duke@1 182 }
duke@1 183
jjg@103 184 directories = Collections.<RelativeDirectory, DirectoryEntry>emptyMap();
jjg@103 185 allDirs = Collections.<RelativeDirectory>emptySet();
duke@1 186
duke@1 187 try {
duke@1 188 openFile();
duke@1 189 long totalLength = zipRandomFile.length();
duke@1 190 ZipDirectory directory = new ZipDirectory(zipRandomFile, 0L, totalLength, this);
duke@1 191 directory.buildIndex();
duke@1 192 } finally {
duke@1 193 if (zipRandomFile != null) {
duke@1 194 closeFile();
duke@1 195 }
duke@1 196 }
duke@1 197
duke@1 198 lastReferenceTimeStamp = System.currentTimeMillis();
duke@1 199 }
duke@1 200
duke@1 201 private void openFile() throws FileNotFoundException {
duke@1 202 if (zipRandomFile == null && zipFile != null) {
duke@1 203 zipRandomFile = new RandomAccessFile(zipFile, "r");
duke@1 204 }
duke@1 205 }
duke@1 206
duke@1 207 private void cleanupState() {
duke@1 208 // Make sure there is a valid but empty index if the file doesn't exist
jjg@57 209 entries = Entry.EMPTY_ARRAY;
jjg@103 210 directories = Collections.<RelativeDirectory, DirectoryEntry>emptyMap();
duke@1 211 zipFileLastModified = NOT_MODIFIED;
jjg@103 212 allDirs = Collections.<RelativeDirectory>emptySet();
duke@1 213 }
duke@1 214
jjg@839 215 public synchronized void close() {
jjg@839 216 writeIndex();
jjg@839 217 closeFile();
duke@1 218 }
duke@1 219
duke@1 220 private void closeFile() {
duke@1 221 if (zipRandomFile != null) {
duke@1 222 try {
duke@1 223 zipRandomFile.close();
duke@1 224 } catch (IOException ex) {
duke@1 225 }
duke@1 226 zipRandomFile = null;
duke@1 227 }
duke@1 228 }
duke@1 229
duke@1 230 /**
jjg@839 231 * Returns the ZipFileIndexEntry for a path, if there is one.
duke@1 232 */
jjg@839 233 synchronized Entry getZipIndexEntry(RelativePath path) {
duke@1 234 try {
duke@1 235 checkIndex();
jjg@103 236 DirectoryEntry de = directories.get(path.dirname());
jjg@103 237 String lookFor = path.basename();
jjg@839 238 return (de == null) ? null : de.getEntry(lookFor);
duke@1 239 }
duke@1 240 catch (IOException e) {
duke@1 241 return null;
duke@1 242 }
duke@1 243 }
duke@1 244
duke@1 245 /**
jjg@839 246 * Returns a javac List of filenames within a directory in the ZipFileIndex.
duke@1 247 */
jjg@839 248 public synchronized com.sun.tools.javac.util.List<String> getFiles(RelativeDirectory path) {
duke@1 249 try {
duke@1 250 checkIndex();
duke@1 251
duke@1 252 DirectoryEntry de = directories.get(path);
duke@1 253 com.sun.tools.javac.util.List<String> ret = de == null ? null : de.getFiles();
duke@1 254
duke@1 255 if (ret == null) {
duke@1 256 return com.sun.tools.javac.util.List.<String>nil();
duke@1 257 }
duke@1 258 return ret;
duke@1 259 }
duke@1 260 catch (IOException e) {
duke@1 261 return com.sun.tools.javac.util.List.<String>nil();
duke@1 262 }
duke@1 263 }
duke@1 264
jjg@839 265 public synchronized List<String> getDirectories(RelativeDirectory path) {
duke@1 266 try {
duke@1 267 checkIndex();
duke@1 268
duke@1 269 DirectoryEntry de = directories.get(path);
duke@1 270 com.sun.tools.javac.util.List<String> ret = de == null ? null : de.getDirectories();
duke@1 271
duke@1 272 if (ret == null) {
duke@1 273 return com.sun.tools.javac.util.List.<String>nil();
duke@1 274 }
duke@1 275
duke@1 276 return ret;
duke@1 277 }
duke@1 278 catch (IOException e) {
duke@1 279 return com.sun.tools.javac.util.List.<String>nil();
duke@1 280 }
duke@1 281 }
duke@1 282
jjg@839 283 public synchronized Set<RelativeDirectory> getAllDirectories() {
duke@1 284 try {
duke@1 285 checkIndex();
duke@1 286 if (allDirs == Collections.EMPTY_SET) {
mcimadamore@1279 287 allDirs = new java.util.LinkedHashSet<RelativeDirectory>(directories.keySet());
duke@1 288 }
duke@1 289
duke@1 290 return allDirs;
duke@1 291 }
duke@1 292 catch (IOException e) {
jjg@103 293 return Collections.<RelativeDirectory>emptySet();
duke@1 294 }
duke@1 295 }
duke@1 296
duke@1 297 /**
duke@1 298 * Tests if a specific path exists in the zip. This method will return true
duke@1 299 * for file entries and directories.
duke@1 300 *
duke@1 301 * @param path A path within the zip.
duke@1 302 * @return True if the path is a file or dir, false otherwise.
duke@1 303 */
jjg@839 304 public synchronized boolean contains(RelativePath path) {
duke@1 305 try {
duke@1 306 checkIndex();
duke@1 307 return getZipIndexEntry(path) != null;
duke@1 308 }
duke@1 309 catch (IOException e) {
duke@1 310 return false;
duke@1 311 }
jjg@839 312 }
jjg@839 313
jjg@839 314 public synchronized boolean isDirectory(RelativePath path) throws IOException {
jjg@839 315 // The top level in a zip file is always a directory.
jjg@839 316 if (path.getPath().length() == 0) {
jjg@839 317 lastReferenceTimeStamp = System.currentTimeMillis();
jjg@839 318 return true;
jjg@839 319 }
jjg@839 320
jjg@839 321 checkIndex();
jjg@839 322 return directories.get(path) != null;
jjg@839 323 }
jjg@839 324
jjg@839 325 public synchronized long getLastModified(RelativeFile path) throws IOException {
jjg@839 326 Entry entry = getZipIndexEntry(path);
jjg@839 327 if (entry == null)
jjg@839 328 throw new FileNotFoundException();
jjg@839 329 return entry.getLastModified();
jjg@839 330 }
jjg@839 331
jjg@839 332 public synchronized int length(RelativeFile path) throws IOException {
jjg@839 333 Entry entry = getZipIndexEntry(path);
jjg@839 334 if (entry == null)
jjg@839 335 throw new FileNotFoundException();
jjg@839 336
jjg@839 337 if (entry.isDir) {
jjg@839 338 return 0;
jjg@839 339 }
jjg@839 340
jjg@839 341 byte[] header = getHeader(entry);
jjg@839 342 // entry is not compressed?
jjg@839 343 if (get2ByteLittleEndian(header, 8) == 0) {
jjg@839 344 return entry.compressedSize;
jjg@839 345 } else {
jjg@839 346 return entry.size;
duke@1 347 }
duke@1 348 }
duke@1 349
jjg@839 350 public synchronized byte[] read(RelativeFile path) throws IOException {
jjg@839 351 Entry entry = getZipIndexEntry(path);
jjg@839 352 if (entry == null)
jjg@839 353 throw new FileNotFoundException("Path not found in ZIP: " + path.path);
jjg@839 354 return read(entry);
duke@1 355 }
duke@1 356
jjg@839 357 synchronized byte[] read(Entry entry) throws IOException {
jjg@839 358 openFile();
jjg@839 359 byte[] result = readBytes(entry);
jjg@839 360 closeFile();
jjg@839 361 return result;
duke@1 362 }
duke@1 363
jjg@839 364 public synchronized int read(RelativeFile path, byte[] buffer) throws IOException {
jjg@839 365 Entry entry = getZipIndexEntry(path);
jjg@839 366 if (entry == null)
jjg@839 367 throw new FileNotFoundException();
jjg@839 368 return read(entry, buffer);
duke@1 369 }
duke@1 370
jjg@839 371 synchronized int read(Entry entry, byte[] buffer)
duke@1 372 throws IOException {
jjg@839 373 int result = readBytes(entry, buffer);
jjg@839 374 return result;
duke@1 375 }
duke@1 376
jjg@57 377 private byte[] readBytes(Entry entry) throws IOException {
duke@1 378 byte[] header = getHeader(entry);
duke@1 379 int csize = entry.compressedSize;
duke@1 380 byte[] cbuf = new byte[csize];
duke@1 381 zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
duke@1 382 zipRandomFile.readFully(cbuf, 0, csize);
duke@1 383
duke@1 384 // is this compressed - offset 8 in the ZipEntry header
duke@1 385 if (get2ByteLittleEndian(header, 8) == 0)
duke@1 386 return cbuf;
duke@1 387
duke@1 388 int size = entry.size;
duke@1 389 byte[] buf = new byte[size];
duke@1 390 if (inflate(cbuf, buf) != size)
duke@1 391 throw new ZipException("corrupted zip file");
duke@1 392
duke@1 393 return buf;
duke@1 394 }
duke@1 395
duke@1 396 /**
duke@1 397 *
duke@1 398 */
jjg@57 399 private int readBytes(Entry entry, byte[] buffer) throws IOException {
duke@1 400 byte[] header = getHeader(entry);
duke@1 401
duke@1 402 // entry is not compressed?
duke@1 403 if (get2ByteLittleEndian(header, 8) == 0) {
duke@1 404 zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
duke@1 405 int offset = 0;
duke@1 406 int size = buffer.length;
duke@1 407 while (offset < size) {
duke@1 408 int count = zipRandomFile.read(buffer, offset, size - offset);
duke@1 409 if (count == -1)
duke@1 410 break;
duke@1 411 offset += count;
duke@1 412 }
duke@1 413 return entry.size;
duke@1 414 }
duke@1 415
duke@1 416 int csize = entry.compressedSize;
duke@1 417 byte[] cbuf = new byte[csize];
duke@1 418 zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
duke@1 419 zipRandomFile.readFully(cbuf, 0, csize);
duke@1 420
duke@1 421 int count = inflate(cbuf, buffer);
duke@1 422 if (count == -1)
duke@1 423 throw new ZipException("corrupted zip file");
duke@1 424
duke@1 425 return entry.size;
duke@1 426 }
duke@1 427
duke@1 428 //----------------------------------------------------------------------------
duke@1 429 // Zip utilities
duke@1 430 //----------------------------------------------------------------------------
duke@1 431
jjg@57 432 private byte[] getHeader(Entry entry) throws IOException {
duke@1 433 zipRandomFile.seek(entry.offset);
duke@1 434 byte[] header = new byte[30];
duke@1 435 zipRandomFile.readFully(header);
duke@1 436 if (get4ByteLittleEndian(header, 0) != 0x04034b50)
duke@1 437 throw new ZipException("corrupted zip file");
duke@1 438 if ((get2ByteLittleEndian(header, 6) & 1) != 0)
duke@1 439 throw new ZipException("encrypted zip file"); // offset 6 in the header of the ZipFileEntry
duke@1 440 return header;
duke@1 441 }
duke@1 442
duke@1 443 /*
duke@1 444 * Inflate using the java.util.zip.Inflater class
duke@1 445 */
jjg@839 446 private SoftReference<Inflater> inflaterRef;
duke@1 447 private int inflate(byte[] src, byte[] dest) {
jjg@839 448 Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());
duke@1 449
duke@1 450 // construct the inflater object or reuse an existing one
duke@1 451 if (inflater == null)
jjg@839 452 inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));
duke@1 453
jjg@839 454 inflater.reset();
jjg@839 455 inflater.setInput(src);
jjg@839 456 try {
jjg@839 457 return inflater.inflate(dest);
jjg@839 458 } catch (DataFormatException ex) {
jjg@839 459 return -1;
duke@1 460 }
duke@1 461 }
duke@1 462
duke@1 463 /**
duke@1 464 * return the two bytes buf[pos], buf[pos+1] as an unsigned integer in little
duke@1 465 * endian format.
duke@1 466 */
duke@1 467 private static int get2ByteLittleEndian(byte[] buf, int pos) {
duke@1 468 return (buf[pos] & 0xFF) + ((buf[pos+1] & 0xFF) << 8);
duke@1 469 }
duke@1 470
duke@1 471 /**
duke@1 472 * return the 4 bytes buf[i..i+3] as an integer in little endian format.
duke@1 473 */
duke@1 474 private static int get4ByteLittleEndian(byte[] buf, int pos) {
duke@1 475 return (buf[pos] & 0xFF) + ((buf[pos + 1] & 0xFF) << 8) +
duke@1 476 ((buf[pos + 2] & 0xFF) << 16) + ((buf[pos + 3] & 0xFF) << 24);
duke@1 477 }
duke@1 478
duke@1 479 /* ----------------------------------------------------------------------------
duke@1 480 * ZipDirectory
duke@1 481 * ----------------------------------------------------------------------------*/
duke@1 482
duke@1 483 private class ZipDirectory {
jjg@103 484 private RelativeDirectory lastDir;
duke@1 485 private int lastStart;
duke@1 486 private int lastLen;
duke@1 487
duke@1 488 byte[] zipDir;
duke@1 489 RandomAccessFile zipRandomFile = null;
duke@1 490 ZipFileIndex zipFileIndex = null;
duke@1 491
duke@1 492 public ZipDirectory(RandomAccessFile zipRandomFile, long start, long end, ZipFileIndex index) throws IOException {
duke@1 493 this.zipRandomFile = zipRandomFile;
duke@1 494 this.zipFileIndex = index;
ksrini@923 495 hasValidHeader();
ksrini@923 496 findCENRecord(start, end);
ksrini@923 497 }
duke@1 498
ksrini@923 499 /*
ksrini@923 500 * the zip entry signature should be at offset 0, otherwise allow the
ksrini@923 501 * calling logic to take evasive action by throwing ZipFormatException.
ksrini@923 502 */
ksrini@923 503 private boolean hasValidHeader() throws IOException {
ksrini@923 504 final long pos = zipRandomFile.getFilePointer();
ksrini@923 505 try {
ksrini@923 506 if (zipRandomFile.read() == 'P') {
ksrini@923 507 if (zipRandomFile.read() == 'K') {
ksrini@923 508 if (zipRandomFile.read() == 0x03) {
ksrini@923 509 if (zipRandomFile.read() == 0x04) {
ksrini@923 510 return true;
ksrini@923 511 }
ksrini@923 512 }
ksrini@923 513 }
ksrini@923 514 }
ksrini@923 515 } finally {
ksrini@923 516 zipRandomFile.seek(pos);
ksrini@923 517 }
ksrini@923 518 throw new ZipFormatException("invalid zip magic");
duke@1 519 }
duke@1 520
duke@1 521 /*
duke@1 522 * Reads zip file central directory.
duke@1 523 * For more details see readCEN in zip_util.c from the JDK sources.
duke@1 524 * This is a Java port of that function.
duke@1 525 */
duke@1 526 private void findCENRecord(long start, long end) throws IOException {
duke@1 527 long totalLength = end - start;
duke@1 528 int endbuflen = 1024;
duke@1 529 byte[] endbuf = new byte[endbuflen];
duke@1 530 long endbufend = end - start;
duke@1 531
duke@1 532 // There is a variable-length field after the dir offset record. We need to do consequential search.
duke@1 533 while (endbufend >= 22) {
duke@1 534 if (endbufend < endbuflen)
duke@1 535 endbuflen = (int)endbufend;
duke@1 536 long endbufpos = endbufend - endbuflen;
duke@1 537 zipRandomFile.seek(start + endbufpos);
duke@1 538 zipRandomFile.readFully(endbuf, 0, endbuflen);
duke@1 539 int i = endbuflen - 22;
duke@1 540 while (i >= 0 &&
duke@1 541 !(endbuf[i] == 0x50 &&
duke@1 542 endbuf[i + 1] == 0x4b &&
duke@1 543 endbuf[i + 2] == 0x05 &&
duke@1 544 endbuf[i + 3] == 0x06 &&
duke@1 545 endbufpos + i + 22 +
duke@1 546 get2ByteLittleEndian(endbuf, i + 20) == totalLength)) {
duke@1 547 i--;
duke@1 548 }
duke@1 549
duke@1 550 if (i >= 0) {
vromero@1467 551 zipDir = new byte[get4ByteLittleEndian(endbuf, i + 12)];
ksrini@923 552 int sz = get4ByteLittleEndian(endbuf, i + 16);
ksrini@923 553 // a negative offset or the entries field indicates a
ksrini@923 554 // potential zip64 archive
vromero@1467 555 if (sz < 0 || get2ByteLittleEndian(endbuf, i + 10) == 0xffff) {
ksrini@923 556 throw new ZipFormatException("detected a zip64 archive");
ksrini@923 557 }
ksrini@923 558 zipRandomFile.seek(start + sz);
vromero@1467 559 zipRandomFile.readFully(zipDir, 0, zipDir.length);
duke@1 560 return;
duke@1 561 } else {
duke@1 562 endbufend = endbufpos + 21;
duke@1 563 }
duke@1 564 }
duke@1 565 throw new ZipException("cannot read zip file");
duke@1 566 }
jjg@103 567
duke@1 568 private void buildIndex() throws IOException {
vromero@1467 569 int len = zipDir.length;
duke@1 570
duke@1 571 // Add each of the files
vromero@1467 572 if (len > 0) {
mcimadamore@1279 573 directories = new LinkedHashMap<RelativeDirectory, DirectoryEntry>();
jjg@57 574 ArrayList<Entry> entryList = new ArrayList<Entry>();
vromero@1467 575 for (int pos = 0; pos < len; ) {
duke@1 576 pos = readEntry(pos, entryList, directories);
duke@1 577 }
duke@1 578
duke@1 579 // Add the accumulated dirs into the same list
jjg@103 580 for (RelativeDirectory d: directories.keySet()) {
jjg@103 581 // use shared RelativeDirectory objects for parent dirs
jjg@103 582 RelativeDirectory parent = getRelativeDirectory(d.dirname().getPath());
jjg@103 583 String file = d.basename();
jjg@103 584 Entry zipFileIndexEntry = new Entry(parent, file);
duke@1 585 zipFileIndexEntry.isDir = true;
duke@1 586 entryList.add(zipFileIndexEntry);
duke@1 587 }
duke@1 588
jjg@57 589 entries = entryList.toArray(new Entry[entryList.size()]);
duke@1 590 Arrays.sort(entries);
duke@1 591 } else {
duke@1 592 cleanupState();
duke@1 593 }
duke@1 594 }
duke@1 595
jjg@57 596 private int readEntry(int pos, List<Entry> entryList,
jjg@103 597 Map<RelativeDirectory, DirectoryEntry> directories) throws IOException {
duke@1 598 if (get4ByteLittleEndian(zipDir, pos) != 0x02014b50) {
duke@1 599 throw new ZipException("cannot read zip file entry");
duke@1 600 }
duke@1 601
duke@1 602 int dirStart = pos + 46;
duke@1 603 int fileStart = dirStart;
duke@1 604 int fileEnd = fileStart + get2ByteLittleEndian(zipDir, pos + 28);
duke@1 605
duke@1 606 if (zipFileIndex.symbolFilePrefixLength != 0 &&
duke@1 607 ((fileEnd - fileStart) >= symbolFilePrefixLength)) {
duke@1 608 dirStart += zipFileIndex.symbolFilePrefixLength;
duke@1 609 fileStart += zipFileIndex.symbolFilePrefixLength;
duke@1 610 }
jjg@103 611 // Force any '\' to '/'. Keep the position of the last separator.
duke@1 612 for (int index = fileStart; index < fileEnd; index++) {
duke@1 613 byte nextByte = zipDir[index];
jjg@103 614 if (nextByte == (byte)'\\') {
jjg@103 615 zipDir[index] = (byte)'/';
jjg@103 616 fileStart = index + 1;
jjg@103 617 } else if (nextByte == (byte)'/') {
duke@1 618 fileStart = index + 1;
duke@1 619 }
duke@1 620 }
duke@1 621
jjg@103 622 RelativeDirectory directory = null;
duke@1 623 if (fileStart == dirStart)
jjg@103 624 directory = getRelativeDirectory("");
duke@1 625 else if (lastDir != null && lastLen == fileStart - dirStart - 1) {
duke@1 626 int index = lastLen - 1;
duke@1 627 while (zipDir[lastStart + index] == zipDir[dirStart + index]) {
duke@1 628 if (index == 0) {
duke@1 629 directory = lastDir;
duke@1 630 break;
duke@1 631 }
duke@1 632 index--;
duke@1 633 }
duke@1 634 }
duke@1 635
duke@1 636 // Sub directories
duke@1 637 if (directory == null) {
duke@1 638 lastStart = dirStart;
duke@1 639 lastLen = fileStart - dirStart - 1;
duke@1 640
jjg@103 641 directory = getRelativeDirectory(new String(zipDir, dirStart, lastLen, "UTF-8"));
duke@1 642 lastDir = directory;
duke@1 643
duke@1 644 // Enter also all the parent directories
jjg@103 645 RelativeDirectory tempDirectory = directory;
duke@1 646
duke@1 647 while (directories.get(tempDirectory) == null) {
duke@1 648 directories.put(tempDirectory, new DirectoryEntry(tempDirectory, zipFileIndex));
jjg@103 649 if (tempDirectory.path.indexOf("/") == tempDirectory.path.length() - 1)
duke@1 650 break;
jjg@103 651 else {
jjg@103 652 // use shared RelativeDirectory objects for parent dirs
jjg@103 653 tempDirectory = getRelativeDirectory(tempDirectory.dirname().getPath());
jjg@103 654 }
duke@1 655 }
duke@1 656 }
duke@1 657 else {
duke@1 658 if (directories.get(directory) == null) {
duke@1 659 directories.put(directory, new DirectoryEntry(directory, zipFileIndex));
duke@1 660 }
duke@1 661 }
duke@1 662
duke@1 663 // For each dir create also a file
duke@1 664 if (fileStart != fileEnd) {
jjg@57 665 Entry entry = new Entry(directory,
duke@1 666 new String(zipDir, fileStart, fileEnd - fileStart, "UTF-8"));
duke@1 667
duke@1 668 entry.setNativeTime(get4ByteLittleEndian(zipDir, pos + 12));
duke@1 669 entry.compressedSize = get4ByteLittleEndian(zipDir, pos + 20);
duke@1 670 entry.size = get4ByteLittleEndian(zipDir, pos + 24);
duke@1 671 entry.offset = get4ByteLittleEndian(zipDir, pos + 42);
duke@1 672 entryList.add(entry);
duke@1 673 }
duke@1 674
duke@1 675 return pos + 46 +
duke@1 676 get2ByteLittleEndian(zipDir, pos + 28) +
duke@1 677 get2ByteLittleEndian(zipDir, pos + 30) +
duke@1 678 get2ByteLittleEndian(zipDir, pos + 32);
duke@1 679 }
duke@1 680 }
duke@1 681
duke@1 682 /**
duke@1 683 * Returns the last modified timestamp of a zip file.
duke@1 684 * @return long
duke@1 685 */
duke@1 686 public long getZipFileLastModified() throws IOException {
jjg@839 687 synchronized (this) {
duke@1 688 checkIndex();
duke@1 689 return zipFileLastModified;
duke@1 690 }
duke@1 691 }
duke@1 692
duke@1 693 /** ------------------------------------------------------------------------
duke@1 694 * DirectoryEntry class
duke@1 695 * -------------------------------------------------------------------------*/
jjg@57 696
duke@1 697 static class DirectoryEntry {
duke@1 698 private boolean filesInited;
duke@1 699 private boolean directoriesInited;
duke@1 700 private boolean zipFileEntriesInited;
duke@1 701 private boolean entriesInited;
duke@1 702
duke@1 703 private long writtenOffsetOffset = 0;
duke@1 704
jjg@103 705 private RelativeDirectory dirName;
duke@1 706
duke@1 707 private com.sun.tools.javac.util.List<String> zipFileEntriesFiles = com.sun.tools.javac.util.List.<String>nil();
duke@1 708 private com.sun.tools.javac.util.List<String> zipFileEntriesDirectories = com.sun.tools.javac.util.List.<String>nil();
jjg@57 709 private com.sun.tools.javac.util.List<Entry> zipFileEntries = com.sun.tools.javac.util.List.<Entry>nil();
duke@1 710
jjg@57 711 private List<Entry> entries = new ArrayList<Entry>();
duke@1 712
duke@1 713 private ZipFileIndex zipFileIndex;
duke@1 714
duke@1 715 private int numEntries;
duke@1 716
jjg@103 717 DirectoryEntry(RelativeDirectory dirName, ZipFileIndex index) {
jjg@103 718 filesInited = false;
duke@1 719 directoriesInited = false;
duke@1 720 entriesInited = false;
duke@1 721
jjg@103 722 this.dirName = dirName;
duke@1 723 this.zipFileIndex = index;
duke@1 724 }
duke@1 725
duke@1 726 private com.sun.tools.javac.util.List<String> getFiles() {
jjg@103 727 if (!filesInited) {
jjg@103 728 initEntries();
jjg@103 729 for (Entry e : entries) {
jjg@103 730 if (!e.isDir) {
jjg@103 731 zipFileEntriesFiles = zipFileEntriesFiles.append(e.name);
jjg@103 732 }
jjg@103 733 }
jjg@103 734 filesInited = true;
duke@1 735 }
duke@1 736 return zipFileEntriesFiles;
duke@1 737 }
duke@1 738
duke@1 739 private com.sun.tools.javac.util.List<String> getDirectories() {
jjg@103 740 if (!directoriesInited) {
jjg@103 741 initEntries();
jjg@103 742 for (Entry e : entries) {
jjg@103 743 if (e.isDir) {
jjg@103 744 zipFileEntriesDirectories = zipFileEntriesDirectories.append(e.name);
jjg@103 745 }
jjg@103 746 }
jjg@103 747 directoriesInited = true;
duke@1 748 }
duke@1 749 return zipFileEntriesDirectories;
duke@1 750 }
duke@1 751
jjg@57 752 private com.sun.tools.javac.util.List<Entry> getEntries() {
jjg@103 753 if (!zipFileEntriesInited) {
jjg@103 754 initEntries();
jjg@103 755 zipFileEntries = com.sun.tools.javac.util.List.nil();
jjg@103 756 for (Entry zfie : entries) {
jjg@103 757 zipFileEntries = zipFileEntries.append(zfie);
jjg@103 758 }
jjg@103 759 zipFileEntriesInited = true;
duke@1 760 }
duke@1 761 return zipFileEntries;
duke@1 762 }
duke@1 763
jjg@57 764 private Entry getEntry(String rootName) {
duke@1 765 initEntries();
jjg@57 766 int index = Collections.binarySearch(entries, new Entry(dirName, rootName));
duke@1 767 if (index < 0) {
duke@1 768 return null;
duke@1 769 }
duke@1 770
duke@1 771 return entries.get(index);
duke@1 772 }
duke@1 773
duke@1 774 private void initEntries() {
duke@1 775 if (entriesInited) {
duke@1 776 return;
duke@1 777 }
duke@1 778
duke@1 779 if (!zipFileIndex.readFromIndex) {
duke@1 780 int from = -Arrays.binarySearch(zipFileIndex.entries,
jjg@57 781 new Entry(dirName, ZipFileIndex.MIN_CHAR)) - 1;
duke@1 782 int to = -Arrays.binarySearch(zipFileIndex.entries,
jjg@57 783 new Entry(dirName, MAX_CHAR)) - 1;
duke@1 784
duke@1 785 for (int i = from; i < to; i++) {
duke@1 786 entries.add(zipFileIndex.entries[i]);
duke@1 787 }
duke@1 788 } else {
duke@1 789 File indexFile = zipFileIndex.getIndexFile();
duke@1 790 if (indexFile != null) {
duke@1 791 RandomAccessFile raf = null;
duke@1 792 try {
duke@1 793 raf = new RandomAccessFile(indexFile, "r");
duke@1 794 raf.seek(writtenOffsetOffset);
duke@1 795
duke@1 796 for (int nFiles = 0; nFiles < numEntries; nFiles++) {
duke@1 797 // Read the name bytes
duke@1 798 int zfieNameBytesLen = raf.readInt();
duke@1 799 byte [] zfieNameBytes = new byte[zfieNameBytesLen];
duke@1 800 raf.read(zfieNameBytes);
duke@1 801 String eName = new String(zfieNameBytes, "UTF-8");
duke@1 802
duke@1 803 // Read isDir
duke@1 804 boolean eIsDir = raf.readByte() == (byte)0 ? false : true;
duke@1 805
duke@1 806 // Read offset of bytes in the real Jar/Zip file
duke@1 807 int eOffset = raf.readInt();
duke@1 808
duke@1 809 // Read size of the file in the real Jar/Zip file
duke@1 810 int eSize = raf.readInt();
duke@1 811
duke@1 812 // Read compressed size of the file in the real Jar/Zip file
duke@1 813 int eCsize = raf.readInt();
duke@1 814
duke@1 815 // Read java time stamp of the file in the real Jar/Zip file
duke@1 816 long eJavaTimestamp = raf.readLong();
duke@1 817
jjg@57 818 Entry rfie = new Entry(dirName, eName);
duke@1 819 rfie.isDir = eIsDir;
duke@1 820 rfie.offset = eOffset;
duke@1 821 rfie.size = eSize;
duke@1 822 rfie.compressedSize = eCsize;
duke@1 823 rfie.javatime = eJavaTimestamp;
duke@1 824 entries.add(rfie);
duke@1 825 }
duke@1 826 } catch (Throwable t) {
duke@1 827 // Do nothing
duke@1 828 } finally {
duke@1 829 try {
jjg@444 830 if (raf != null) {
duke@1 831 raf.close();
duke@1 832 }
duke@1 833 } catch (Throwable t) {
duke@1 834 // Do nothing
duke@1 835 }
duke@1 836 }
duke@1 837 }
duke@1 838 }
duke@1 839
duke@1 840 entriesInited = true;
duke@1 841 }
duke@1 842
jjg@57 843 List<Entry> getEntriesAsCollection() {
duke@1 844 initEntries();
duke@1 845
duke@1 846 return entries;
duke@1 847 }
duke@1 848 }
duke@1 849
duke@1 850 private boolean readIndex() {
duke@1 851 if (triedToReadIndex || !usePreindexedCache) {
duke@1 852 return false;
duke@1 853 }
duke@1 854
duke@1 855 boolean ret = false;
jjg@839 856 synchronized (this) {
duke@1 857 triedToReadIndex = true;
duke@1 858 RandomAccessFile raf = null;
duke@1 859 try {
duke@1 860 File indexFileName = getIndexFile();
duke@1 861 raf = new RandomAccessFile(indexFileName, "r");
duke@1 862
duke@1 863 long fileStamp = raf.readLong();
duke@1 864 if (zipFile.lastModified() != fileStamp) {
duke@1 865 ret = false;
duke@1 866 } else {
mcimadamore@1279 867 directories = new LinkedHashMap<RelativeDirectory, DirectoryEntry>();
duke@1 868 int numDirs = raf.readInt();
duke@1 869 for (int nDirs = 0; nDirs < numDirs; nDirs++) {
duke@1 870 int dirNameBytesLen = raf.readInt();
duke@1 871 byte [] dirNameBytes = new byte[dirNameBytesLen];
duke@1 872 raf.read(dirNameBytes);
duke@1 873
jjg@103 874 RelativeDirectory dirNameStr = getRelativeDirectory(new String(dirNameBytes, "UTF-8"));
duke@1 875 DirectoryEntry de = new DirectoryEntry(dirNameStr, this);
duke@1 876 de.numEntries = raf.readInt();
duke@1 877 de.writtenOffsetOffset = raf.readLong();
duke@1 878 directories.put(dirNameStr, de);
duke@1 879 }
duke@1 880 ret = true;
duke@1 881 zipFileLastModified = fileStamp;
duke@1 882 }
duke@1 883 } catch (Throwable t) {
duke@1 884 // Do nothing
duke@1 885 } finally {
duke@1 886 if (raf != null) {
duke@1 887 try {
duke@1 888 raf.close();
duke@1 889 } catch (Throwable tt) {
duke@1 890 // Do nothing
duke@1 891 }
duke@1 892 }
duke@1 893 }
duke@1 894 if (ret == true) {
duke@1 895 readFromIndex = true;
duke@1 896 }
duke@1 897 }
duke@1 898
duke@1 899 return ret;
duke@1 900 }
duke@1 901
duke@1 902 private boolean writeIndex() {
duke@1 903 boolean ret = false;
duke@1 904 if (readFromIndex || !usePreindexedCache) {
duke@1 905 return true;
duke@1 906 }
duke@1 907
duke@1 908 if (!writeIndex) {
duke@1 909 return true;
duke@1 910 }
duke@1 911
duke@1 912 File indexFile = getIndexFile();
duke@1 913 if (indexFile == null) {
duke@1 914 return false;
duke@1 915 }
duke@1 916
duke@1 917 RandomAccessFile raf = null;
duke@1 918 long writtenSoFar = 0;
duke@1 919 try {
duke@1 920 raf = new RandomAccessFile(indexFile, "rw");
duke@1 921
duke@1 922 raf.writeLong(zipFileLastModified);
duke@1 923 writtenSoFar += 8;
duke@1 924
duke@1 925 List<DirectoryEntry> directoriesToWrite = new ArrayList<DirectoryEntry>();
jjg@103 926 Map<RelativeDirectory, Long> offsets = new HashMap<RelativeDirectory, Long>();
duke@1 927 raf.writeInt(directories.keySet().size());
duke@1 928 writtenSoFar += 4;
duke@1 929
jjg@103 930 for (RelativeDirectory dirName: directories.keySet()) {
duke@1 931 DirectoryEntry dirEntry = directories.get(dirName);
duke@1 932
duke@1 933 directoriesToWrite.add(dirEntry);
duke@1 934
duke@1 935 // Write the dir name bytes
jjg@103 936 byte [] dirNameBytes = dirName.getPath().getBytes("UTF-8");
duke@1 937 int dirNameBytesLen = dirNameBytes.length;
duke@1 938 raf.writeInt(dirNameBytesLen);
duke@1 939 writtenSoFar += 4;
duke@1 940
duke@1 941 raf.write(dirNameBytes);
duke@1 942 writtenSoFar += dirNameBytesLen;
duke@1 943
duke@1 944 // Write the number of files in the dir
mcimadamore@184 945 List<Entry> dirEntries = dirEntry.getEntriesAsCollection();
duke@1 946 raf.writeInt(dirEntries.size());
duke@1 947 writtenSoFar += 4;
duke@1 948
duke@1 949 offsets.put(dirName, new Long(writtenSoFar));
duke@1 950
duke@1 951 // Write the offset of the file's data in the dir
duke@1 952 dirEntry.writtenOffsetOffset = 0L;
duke@1 953 raf.writeLong(0L);
duke@1 954 writtenSoFar += 8;
duke@1 955 }
duke@1 956
duke@1 957 for (DirectoryEntry de : directoriesToWrite) {
duke@1 958 // Fix up the offset in the directory table
duke@1 959 long currFP = raf.getFilePointer();
duke@1 960
duke@1 961 long offsetOffset = offsets.get(de.dirName).longValue();
duke@1 962 raf.seek(offsetOffset);
duke@1 963 raf.writeLong(writtenSoFar);
duke@1 964
duke@1 965 raf.seek(currFP);
duke@1 966
duke@1 967 // Now write each of the files in the DirectoryEntry
jjg@839 968 List<Entry> list = de.getEntriesAsCollection();
jjg@839 969 for (Entry zfie : list) {
duke@1 970 // Write the name bytes
duke@1 971 byte [] zfieNameBytes = zfie.name.getBytes("UTF-8");
duke@1 972 int zfieNameBytesLen = zfieNameBytes.length;
duke@1 973 raf.writeInt(zfieNameBytesLen);
duke@1 974 writtenSoFar += 4;
duke@1 975 raf.write(zfieNameBytes);
duke@1 976 writtenSoFar += zfieNameBytesLen;
duke@1 977
duke@1 978 // Write isDir
duke@1 979 raf.writeByte(zfie.isDir ? (byte)1 : (byte)0);
duke@1 980 writtenSoFar += 1;
duke@1 981
duke@1 982 // Write offset of bytes in the real Jar/Zip file
duke@1 983 raf.writeInt(zfie.offset);
duke@1 984 writtenSoFar += 4;
duke@1 985
duke@1 986 // Write size of the file in the real Jar/Zip file
duke@1 987 raf.writeInt(zfie.size);
duke@1 988 writtenSoFar += 4;
duke@1 989
duke@1 990 // Write compressed size of the file in the real Jar/Zip file
duke@1 991 raf.writeInt(zfie.compressedSize);
duke@1 992 writtenSoFar += 4;
duke@1 993
duke@1 994 // Write java time stamp of the file in the real Jar/Zip file
duke@1 995 raf.writeLong(zfie.getLastModified());
duke@1 996 writtenSoFar += 8;
duke@1 997 }
duke@1 998 }
duke@1 999 } catch (Throwable t) {
duke@1 1000 // Do nothing
duke@1 1001 } finally {
duke@1 1002 try {
duke@1 1003 if (raf != null) {
duke@1 1004 raf.close();
duke@1 1005 }
duke@1 1006 } catch(IOException ioe) {
duke@1 1007 // Do nothing
duke@1 1008 }
duke@1 1009 }
duke@1 1010
duke@1 1011 return ret;
duke@1 1012 }
duke@1 1013
duke@1 1014 public boolean writeZipIndex() {
jjg@839 1015 synchronized (this) {
duke@1 1016 return writeIndex();
duke@1 1017 }
duke@1 1018 }
duke@1 1019
duke@1 1020 private File getIndexFile() {
duke@1 1021 if (zipIndexFile == null) {
duke@1 1022 if (zipFile == null) {
duke@1 1023 return null;
duke@1 1024 }
duke@1 1025
duke@1 1026 zipIndexFile = new File((preindexedCacheLocation == null ? "" : preindexedCacheLocation) +
duke@1 1027 zipFile.getName() + ".index");
duke@1 1028 }
duke@1 1029
duke@1 1030 return zipIndexFile;
duke@1 1031 }
duke@1 1032
duke@1 1033 public File getZipFile() {
duke@1 1034 return zipFile;
duke@1 1035 }
jjg@57 1036
jjg@424 1037 File getAbsoluteFile() {
jjg@424 1038 File absFile = (absFileRef == null ? null : absFileRef.get());
jjg@424 1039 if (absFile == null) {
jjg@424 1040 absFile = zipFile.getAbsoluteFile();
jjg@424 1041 absFileRef = new SoftReference<File>(absFile);
jjg@424 1042 }
jjg@424 1043 return absFile;
jjg@424 1044 }
jjg@424 1045
jjg@103 1046 private RelativeDirectory getRelativeDirectory(String path) {
jjg@103 1047 RelativeDirectory rd;
jjg@103 1048 SoftReference<RelativeDirectory> ref = relativeDirectoryCache.get(path);
jjg@103 1049 if (ref != null) {
jjg@103 1050 rd = ref.get();
jjg@103 1051 if (rd != null)
jjg@103 1052 return rd;
jjg@103 1053 }
jjg@103 1054 rd = new RelativeDirectory(path);
jjg@103 1055 relativeDirectoryCache.put(path, new SoftReference<RelativeDirectory>(rd));
jjg@103 1056 return rd;
jjg@103 1057 }
jjg@57 1058
jjg@57 1059 static class Entry implements Comparable<Entry> {
jjg@57 1060 public static final Entry[] EMPTY_ARRAY = {};
jjg@57 1061
jjg@57 1062 // Directory related
jjg@103 1063 RelativeDirectory dir;
jjg@57 1064 boolean isDir;
jjg@57 1065
jjg@57 1066 // File related
jjg@57 1067 String name;
jjg@57 1068
jjg@57 1069 int offset;
jjg@57 1070 int size;
jjg@57 1071 int compressedSize;
jjg@57 1072 long javatime;
jjg@57 1073
jjg@57 1074 private int nativetime;
jjg@57 1075
jjg@103 1076 public Entry(RelativePath path) {
jjg@103 1077 this(path.dirname(), path.basename());
jjg@57 1078 }
jjg@57 1079
jjg@103 1080 public Entry(RelativeDirectory directory, String name) {
jjg@103 1081 this.dir = directory;
jjg@57 1082 this.name = name;
jjg@57 1083 }
jjg@57 1084
jjg@57 1085 public String getName() {
jjg@103 1086 return new RelativeFile(dir, name).getPath();
jjg@57 1087 }
jjg@57 1088
jjg@57 1089 public String getFileName() {
jjg@57 1090 return name;
jjg@57 1091 }
jjg@57 1092
jjg@57 1093 public long getLastModified() {
jjg@57 1094 if (javatime == 0) {
jjg@57 1095 javatime = dosToJavaTime(nativetime);
jjg@57 1096 }
jjg@57 1097 return javatime;
jjg@57 1098 }
jjg@57 1099
jjg@71 1100 // based on dosToJavaTime in java.util.Zip, but avoiding the
jjg@71 1101 // use of deprecated Date constructor
jjg@71 1102 private static long dosToJavaTime(int dtime) {
jjg@71 1103 Calendar c = Calendar.getInstance();
jjg@71 1104 c.set(Calendar.YEAR, ((dtime >> 25) & 0x7f) + 1980);
jjg@71 1105 c.set(Calendar.MONTH, ((dtime >> 21) & 0x0f) - 1);
jjg@71 1106 c.set(Calendar.DATE, ((dtime >> 16) & 0x1f));
jjg@71 1107 c.set(Calendar.HOUR_OF_DAY, ((dtime >> 11) & 0x1f));
jjg@71 1108 c.set(Calendar.MINUTE, ((dtime >> 5) & 0x3f));
jjg@71 1109 c.set(Calendar.SECOND, ((dtime << 1) & 0x3e));
jjg@71 1110 c.set(Calendar.MILLISECOND, 0);
jjg@71 1111 return c.getTimeInMillis();
jjg@57 1112 }
jjg@57 1113
jjg@57 1114 void setNativeTime(int natTime) {
jjg@57 1115 nativetime = natTime;
jjg@57 1116 }
jjg@57 1117
jjg@57 1118 public boolean isDirectory() {
jjg@57 1119 return isDir;
jjg@57 1120 }
jjg@57 1121
jjg@57 1122 public int compareTo(Entry other) {
jjg@103 1123 RelativeDirectory otherD = other.dir;
jjg@57 1124 if (dir != otherD) {
jjg@57 1125 int c = dir.compareTo(otherD);
jjg@57 1126 if (c != 0)
jjg@57 1127 return c;
jjg@57 1128 }
jjg@57 1129 return name.compareTo(other.name);
jjg@57 1130 }
jjg@57 1131
jjg@103 1132 @Override
jjg@103 1133 public boolean equals(Object o) {
jjg@103 1134 if (!(o instanceof Entry))
jjg@103 1135 return false;
jjg@103 1136 Entry other = (Entry) o;
jjg@103 1137 return dir.equals(other.dir) && name.equals(other.name);
jjg@103 1138 }
jjg@103 1139
jjg@103 1140 @Override
jjg@103 1141 public int hashCode() {
jjg@103 1142 int hash = 7;
jjg@103 1143 hash = 97 * hash + (this.dir != null ? this.dir.hashCode() : 0);
jjg@103 1144 hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
jjg@103 1145 return hash;
jjg@103 1146 }
jjg@103 1147
jjg@839 1148 @Override
jjg@57 1149 public String toString() {
jjg@57 1150 return isDir ? ("Dir:" + dir + " : " + name) :
jjg@57 1151 (dir + ":" + name);
jjg@57 1152 }
jjg@57 1153 }
jjg@57 1154
ksrini@923 1155 /*
ksrini@923 1156 * Exception primarily used to implement a failover, used exclusively here.
ksrini@923 1157 */
ksrini@923 1158
ksrini@923 1159 static final class ZipFormatException extends IOException {
ksrini@923 1160 private static final long serialVersionUID = 8000196834066748623L;
ksrini@923 1161 protected ZipFormatException(String message) {
ksrini@923 1162 super(message);
ksrini@923 1163 }
ksrini@923 1164
ksrini@923 1165 protected ZipFormatException(String message, Throwable cause) {
ksrini@923 1166 super(message, cause);
ksrini@923 1167 }
ksrini@923 1168 }
duke@1 1169 }

mercurial