Source for java.util.zip.ZipFile

   1: /* ZipFile.java --
   2:    Copyright (C) 2001, 2002, 2003, 2004, 2005
   3:    Free Software Foundation, Inc.
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11: 
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: 
  39: 
  40: package java.util.zip;
  41: 
  42: import gnu.java.util.EmptyEnumeration;
  43: 
  44: import java.io.BufferedInputStream;
  45: import java.io.DataInput;
  46: import java.io.EOFException;
  47: import java.io.File;
  48: import java.io.IOException;
  49: import java.io.InputStream;
  50: import java.io.RandomAccessFile;
  51: import java.io.UnsupportedEncodingException;
  52: import java.util.Enumeration;
  53: import java.util.HashMap;
  54: import java.util.Iterator;
  55: 
  56: /**
  57:  * This class represents a Zip archive.  You can ask for the contained
  58:  * entries, or get an input stream for a file entry.  The entry is
  59:  * automatically decompressed.
  60:  *
  61:  * This class is thread safe:  You can open input streams for arbitrary
  62:  * entries in different threads.
  63:  *
  64:  * @author Jochen Hoenicke
  65:  * @author Artur Biesiadowski
  66:  */
  67: public class ZipFile implements ZipConstants
  68: {
  69: 
  70:   /**
  71:    * Mode flag to open a zip file for reading.
  72:    */
  73:   public static final int OPEN_READ = 0x1;
  74: 
  75:   /**
  76:    * Mode flag to delete a zip file after reading.
  77:    */
  78:   public static final int OPEN_DELETE = 0x4;
  79: 
  80:   // Name of this zip file.
  81:   private final String name;
  82: 
  83:   // File from which zip entries are read.
  84:   private final RandomAccessFile raf;
  85: 
  86:   // The entries of this zip file when initialized and not yet closed.
  87:   private HashMap entries;
  88: 
  89:   private boolean closed = false;
  90: 
  91:   /**
  92:    * Opens a Zip file with the given name for reading.
  93:    * @exception IOException if a i/o error occured.
  94:    * @exception ZipException if the file doesn't contain a valid zip
  95:    * archive.  
  96:    */
  97:   public ZipFile(String name) throws ZipException, IOException
  98:   {
  99:     this.raf = new RandomAccessFile(name, "r");
 100:     this.name = name;
 101:     checkZipFile();
 102:   }
 103: 
 104:   /**
 105:    * Opens a Zip file reading the given File.
 106:    * @exception IOException if a i/o error occured.
 107:    * @exception ZipException if the file doesn't contain a valid zip
 108:    * archive.  
 109:    */
 110:   public ZipFile(File file) throws ZipException, IOException
 111:   {
 112:     this.raf = new RandomAccessFile(file, "r");
 113:     this.name = file.getPath();
 114:     checkZipFile();
 115:   }
 116: 
 117:   /**
 118:    * Opens a Zip file reading the given File in the given mode.
 119:    *
 120:    * If the OPEN_DELETE mode is specified, the zip file will be deleted at
 121:    * some time moment after it is opened. It will be deleted before the zip
 122:    * file is closed or the Virtual Machine exits.
 123:    * 
 124:    * The contents of the zip file will be accessible until it is closed.
 125:    *
 126:    * @since JDK1.3
 127:    * @param mode Must be one of OPEN_READ or OPEN_READ | OPEN_DELETE
 128:    *
 129:    * @exception IOException if a i/o error occured.
 130:    * @exception ZipException if the file doesn't contain a valid zip
 131:    * archive.  
 132:    */
 133:   public ZipFile(File file, int mode) throws ZipException, IOException
 134:   {
 135:     if (mode != OPEN_READ && mode != (OPEN_READ | OPEN_DELETE))
 136:       throw new IllegalArgumentException("invalid mode");
 137:     if ((mode & OPEN_DELETE) != 0)
 138:       file.deleteOnExit();
 139:     this.raf = new RandomAccessFile(file, "r");
 140:     this.name = file.getPath();
 141:     checkZipFile();
 142:   }
 143: 
 144:   private void checkZipFile() throws IOException, ZipException
 145:   {
 146:     byte[] magicBuf = new byte[4];
 147:     raf.read(magicBuf);
 148: 
 149:     if (readLeInt(magicBuf, 0) != LOCSIG)
 150:       {
 151:     raf.close();
 152:     throw new ZipException("Not a valid zip file");
 153:       }
 154:   }
 155: 
 156:   /**
 157:    * Checks if file is closed and throws an exception.
 158:    */
 159:   private void checkClosed()
 160:   {
 161:     if (closed)
 162:       throw new IllegalStateException("ZipFile has closed: " + name);
 163:   }
 164: 
 165:   /**
 166:    * Read an unsigned short in little endian byte order from the given
 167:    * DataInput stream using the given byte buffer.
 168:    *
 169:    * @param di DataInput stream to read from.
 170:    * @param b the byte buffer to read in (must be at least 2 bytes long).
 171:    * @return The value read.
 172:    *
 173:    * @exception IOException if a i/o error occured.
 174:    * @exception EOFException if the file ends prematurely
 175:    */
 176:   private int readLeShort(DataInput di, byte[] b) throws IOException
 177:   {
 178:     di.readFully(b, 0, 2);
 179:     return (b[0] & 0xff) | (b[1] & 0xff) << 8;
 180:   }
 181: 
 182:   /**
 183:    * Read an int in little endian byte order from the given
 184:    * DataInput stream using the given byte buffer.
 185:    *
 186:    * @param di DataInput stream to read from.
 187:    * @param b the byte buffer to read in (must be at least 4 bytes long).
 188:    * @return The value read.
 189:    *
 190:    * @exception IOException if a i/o error occured.
 191:    * @exception EOFException if the file ends prematurely
 192:    */
 193:   private int readLeInt(DataInput di, byte[] b) throws IOException
 194:   {
 195:     di.readFully(b, 0, 4);
 196:     return ((b[0] & 0xff) | (b[1] & 0xff) << 8)
 197:         | ((b[2] & 0xff) | (b[3] & 0xff) << 8) << 16;
 198:   }
 199: 
 200:   /**
 201:    * Read an unsigned short in little endian byte order from the given
 202:    * byte buffer at the given offset.
 203:    *
 204:    * @param b the byte array to read from.
 205:    * @param off the offset to read from.
 206:    * @return The value read.
 207:    */
 208:   private int readLeShort(byte[] b, int off)
 209:   {
 210:     return (b[off] & 0xff) | (b[off+1] & 0xff) << 8;
 211:   }
 212: 
 213:   /**
 214:    * Read an int in little endian byte order from the given
 215:    * byte buffer at the given offset.
 216:    *
 217:    * @param b the byte array to read from.
 218:    * @param off the offset to read from.
 219:    * @return The value read.
 220:    */
 221:   private int readLeInt(byte[] b, int off)
 222:   {
 223:     return ((b[off] & 0xff) | (b[off+1] & 0xff) << 8)
 224:         | ((b[off+2] & 0xff) | (b[off+3] & 0xff) << 8) << 16;
 225:   }
 226:   
 227: 
 228:   /**
 229:    * Read the central directory of a zip file and fill the entries
 230:    * array.  This is called exactly once when first needed. It is called
 231:    * while holding the lock on <code>raf</code>.
 232:    *
 233:    * @exception IOException if a i/o error occured.
 234:    * @exception ZipException if the central directory is malformed 
 235:    */
 236:   private void readEntries() throws ZipException, IOException
 237:   {
 238:     /* Search for the End Of Central Directory.  When a zip comment is 
 239:      * present the directory may start earlier.
 240:      * FIXME: This searches the whole file in a very slow manner if the
 241:      * file isn't a zip file.
 242:      */
 243:     long pos = raf.length() - ENDHDR;
 244:     byte[] ebs  = new byte[CENHDR];
 245:     
 246:     do
 247:       {
 248:     if (pos < 0)
 249:       throw new ZipException
 250:         ("central directory not found, probably not a zip file: " + name);
 251:     raf.seek(pos--);
 252:       }
 253:     while (readLeInt(raf, ebs) != ENDSIG);
 254:     
 255:     if (raf.skipBytes(ENDTOT - ENDNRD) != ENDTOT - ENDNRD)
 256:       throw new EOFException(name);
 257:     int count = readLeShort(raf, ebs);
 258:     if (raf.skipBytes(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ)
 259:       throw new EOFException(name);
 260:     int centralOffset = readLeInt(raf, ebs);
 261: 
 262:     entries = new HashMap(count+count/2);
 263:     raf.seek(centralOffset);
 264:     
 265:     byte[] buffer = new byte[16];
 266:     for (int i = 0; i < count; i++)
 267:       {
 268:           raf.readFully(ebs);
 269:     if (readLeInt(ebs, 0) != CENSIG)
 270:       throw new ZipException("Wrong Central Directory signature: " + name);
 271: 
 272:     int method = readLeShort(ebs, CENHOW);
 273:     int dostime = readLeInt(ebs, CENTIM);
 274:     int crc = readLeInt(ebs, CENCRC);
 275:     int csize = readLeInt(ebs, CENSIZ);
 276:     int size = readLeInt(ebs, CENLEN);
 277:     int nameLen = readLeShort(ebs, CENNAM);
 278:     int extraLen = readLeShort(ebs, CENEXT);
 279:     int commentLen = readLeShort(ebs, CENCOM);
 280:     
 281:     int offset = readLeInt(ebs, CENOFF);
 282: 
 283:     int needBuffer = Math.max(nameLen, commentLen);
 284:     if (buffer.length < needBuffer)
 285:       buffer = new byte[needBuffer];
 286: 
 287:     raf.readFully(buffer, 0, nameLen);
 288:     String name;
 289:     try
 290:       {
 291:         name = new String(buffer, 0, nameLen, "UTF-8");
 292:       }
 293:     catch (UnsupportedEncodingException uee)
 294:       {
 295:         throw new AssertionError(uee);
 296:       }
 297: 
 298:     ZipEntry entry = new ZipEntry(name);
 299:     entry.setMethod(method);
 300:     entry.setCrc(crc & 0xffffffffL);
 301:     entry.setSize(size & 0xffffffffL);
 302:     entry.setCompressedSize(csize & 0xffffffffL);
 303:     entry.setDOSTime(dostime);
 304:     if (extraLen > 0)
 305:       {
 306:         byte[] extra = new byte[extraLen];
 307:         raf.readFully(extra);
 308:         entry.setExtra(extra);
 309:       }
 310:     if (commentLen > 0)
 311:       {
 312:         raf.readFully(buffer, 0, commentLen);
 313:         try
 314:           {
 315:         entry.setComment(new String(buffer, 0, commentLen, "UTF-8"));
 316:           }
 317:         catch (UnsupportedEncodingException uee)
 318:           {
 319:         throw new AssertionError(uee);
 320:           }
 321:       }
 322:     entry.offset = offset;
 323:     entries.put(name, entry);
 324:       }
 325:   }
 326: 
 327:   /**
 328:    * Closes the ZipFile.  This also closes all input streams given by
 329:    * this class.  After this is called, no further method should be
 330:    * called.
 331:    * 
 332:    * @exception IOException if a i/o error occured.
 333:    */
 334:   public void close() throws IOException
 335:   {
 336:     RandomAccessFile raf = this.raf;
 337:     if (raf == null)
 338:       return;
 339: 
 340:     synchronized (raf)
 341:       {
 342:     closed = true;
 343:     entries = null;
 344:     raf.close();
 345:       }
 346:   }
 347: 
 348:   /**
 349:    * Calls the <code>close()</code> method when this ZipFile has not yet
 350:    * been explicitly closed.
 351:    */
 352:   protected void finalize() throws IOException
 353:   {
 354:     if (!closed && raf != null) close();
 355:   }
 356: 
 357:   /**
 358:    * Returns an enumeration of all Zip entries in this Zip file.
 359:    *
 360:    * @exception IllegalStateException when the ZipFile has already been closed
 361:    */
 362:   public Enumeration entries()
 363:   {
 364:     checkClosed();
 365:     
 366:     try
 367:       {
 368:     return new ZipEntryEnumeration(getEntries().values().iterator());
 369:       }
 370:     catch (IOException ioe)
 371:       {
 372:     return EmptyEnumeration.getInstance();
 373:       }
 374:   }
 375: 
 376:   /**
 377:    * Checks that the ZipFile is still open and reads entries when necessary.
 378:    *
 379:    * @exception IllegalStateException when the ZipFile has already been closed.
 380:    * @exception IOEexception when the entries could not be read.
 381:    */
 382:   private HashMap getEntries() throws IOException
 383:   {
 384:     synchronized(raf)
 385:       {
 386:     checkClosed();
 387: 
 388:     if (entries == null)
 389:       readEntries();
 390: 
 391:     return entries;
 392:       }
 393:   }
 394: 
 395:   /**
 396:    * Searches for a zip entry in this archive with the given name.
 397:    *
 398:    * @param the name. May contain directory components separated by
 399:    * slashes ('/').
 400:    * @return the zip entry, or null if no entry with that name exists.
 401:    *
 402:    * @exception IllegalStateException when the ZipFile has already been closed
 403:    */
 404:   public ZipEntry getEntry(String name)
 405:   {
 406:     checkClosed();
 407: 
 408:     try
 409:       {
 410:     HashMap entries = getEntries();
 411:     ZipEntry entry = (ZipEntry) entries.get(name);
 412:         // If we didn't find it, maybe it's a directory.
 413:         if (entry == null && !name.endsWith("/"))
 414:             entry = (ZipEntry) entries.get(name + '/');
 415:     return entry != null ? new ZipEntry(entry, name) : null;
 416:       }
 417:     catch (IOException ioe)
 418:       {
 419:     return null;
 420:       }
 421:   }
 422: 
 423: 
 424:   //access should be protected by synchronized(raf)
 425:   private byte[] locBuf = new byte[LOCHDR];
 426: 
 427:   /**
 428:    * Checks, if the local header of the entry at index i matches the
 429:    * central directory, and returns the offset to the data.
 430:    * 
 431:    * @param entry to check.
 432:    * @return the start offset of the (compressed) data.
 433:    *
 434:    * @exception IOException if a i/o error occured.
 435:    * @exception ZipException if the local header doesn't match the 
 436:    * central directory header
 437:    */
 438:   private long checkLocalHeader(ZipEntry entry) throws IOException
 439:   {
 440:     synchronized (raf)
 441:       {
 442:     raf.seek(entry.offset);
 443:     raf.readFully(locBuf);
 444:     
 445:     if (readLeInt(locBuf, 0) != LOCSIG)
 446:       throw new ZipException("Wrong Local header signature: " + name);
 447: 
 448:     if (entry.getMethod() != readLeShort(locBuf, LOCHOW))
 449:       throw new ZipException("Compression method mismatch: " + name);
 450: 
 451:     if (entry.getName().length() != readLeShort(locBuf, LOCNAM))
 452:       throw new ZipException("file name length mismatch: " + name);
 453: 
 454:     int extraLen = entry.getName().length() + readLeShort(locBuf, LOCEXT);
 455:     return entry.offset + LOCHDR + extraLen;
 456:       }
 457:   }
 458: 
 459:   /**
 460:    * Creates an input stream reading the given zip entry as
 461:    * uncompressed data.  Normally zip entry should be an entry
 462:    * returned by getEntry() or entries().
 463:    *
 464:    * This implementation returns null if the requested entry does not
 465:    * exist.  This decision is not obviously correct, however, it does
 466:    * appear to mirror Sun's implementation, and it is consistant with
 467:    * their javadoc.  On the other hand, the old JCL book, 2nd Edition,
 468:    * claims that this should return a "non-null ZIP entry".  We have
 469:    * chosen for now ignore the old book, as modern versions of Ant (an
 470:    * important application) depend on this behaviour.  See discussion
 471:    * in this thread:
 472:    * http://gcc.gnu.org/ml/java-patches/2004-q2/msg00602.html
 473:    *
 474:    * @param entry the entry to create an InputStream for.
 475:    * @return the input stream, or null if the requested entry does not exist.
 476:    *
 477:    * @exception IllegalStateException when the ZipFile has already been closed
 478:    * @exception IOException if a i/o error occured.
 479:    * @exception ZipException if the Zip archive is malformed.  
 480:    */
 481:   public InputStream getInputStream(ZipEntry entry) throws IOException
 482:   {
 483:     checkClosed();
 484: 
 485:     HashMap entries = getEntries();
 486:     String name = entry.getName();
 487:     ZipEntry zipEntry = (ZipEntry) entries.get(name);
 488:     if (zipEntry == null)
 489:       return null;
 490: 
 491:     long start = checkLocalHeader(zipEntry);
 492:     int method = zipEntry.getMethod();
 493:     InputStream is = new BufferedInputStream(new PartialInputStream
 494:       (raf, start, zipEntry.getCompressedSize()));
 495:     switch (method)
 496:       {
 497:       case ZipOutputStream.STORED:
 498:     return is;
 499:       case ZipOutputStream.DEFLATED:
 500:     return new InflaterInputStream(is, new Inflater(true));
 501:       default:
 502:     throw new ZipException("Unknown compression method " + method);
 503:       }
 504:   }
 505:   
 506:   /**
 507:    * Returns the (path) name of this zip file.
 508:    */
 509:   public String getName()
 510:   {
 511:     return name;
 512:   }
 513: 
 514:   /**
 515:    * Returns the number of entries in this zip file.
 516:    *
 517:    * @exception IllegalStateException when the ZipFile has already been closed
 518:    */
 519:   public int size()
 520:   {
 521:     checkClosed();
 522:     
 523:     try
 524:       {
 525:     return getEntries().size();
 526:       }
 527:     catch (IOException ioe)
 528:       {
 529:     return 0;
 530:       }
 531:   }
 532:   
 533:   private static class ZipEntryEnumeration implements Enumeration
 534:   {
 535:     private final Iterator elements;
 536: 
 537:     public ZipEntryEnumeration(Iterator elements)
 538:     {
 539:       this.elements = elements;
 540:     }
 541: 
 542:     public boolean hasMoreElements()
 543:     {
 544:       return elements.hasNext();
 545:     }
 546: 
 547:     public Object nextElement()
 548:     {
 549:       /* We return a clone, just to be safe that the user doesn't
 550:        * change the entry.  
 551:        */
 552:       return ((ZipEntry)elements.next()).clone();
 553:     }
 554:   }
 555: 
 556:   private static class PartialInputStream extends InputStream
 557:   {
 558:     private final RandomAccessFile raf;
 559:     long filepos, end;
 560: 
 561:     public PartialInputStream(RandomAccessFile raf, long start, long len)
 562:     {
 563:       this.raf = raf;
 564:       filepos = start;
 565:       end = start + len;
 566:     }
 567:     
 568:     public int available()
 569:     {
 570:       long amount = end - filepos;
 571:       if (amount > Integer.MAX_VALUE)
 572:     return Integer.MAX_VALUE;
 573:       return (int) amount;
 574:     }
 575:     
 576:     public int read() throws IOException
 577:     {
 578:       if (filepos == end)
 579:     return -1;
 580:       synchronized (raf)
 581:     {
 582:       raf.seek(filepos++);
 583:       return raf.read();
 584:     }
 585:     }
 586: 
 587:     public int read(byte[] b, int off, int len) throws IOException
 588:     {
 589:       if (len > end - filepos)
 590:     {
 591:       len = (int) (end - filepos);
 592:       if (len == 0)
 593:         return -1;
 594:     }
 595:       synchronized (raf)
 596:     {
 597:       raf.seek(filepos);
 598:       int count = raf.read(b, off, len);
 599:       if (count > 0)
 600:         filepos += len;
 601:       return count;
 602:     }
 603:     }
 604: 
 605:     public long skip(long amount)
 606:     {
 607:       if (amount < 0)
 608:     throw new IllegalArgumentException();
 609:       if (amount > end - filepos)
 610:     amount = end - filepos;
 611:       filepos += amount;
 612:       return amount;
 613:     }
 614:   }
 615: }