home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / ZipInputStream.java < prev    next >
Text File  |  1997-05-20  |  8KB  |  276 lines

  1. /*
  2.  * @(#)ZipInputStream.java    1.11 97/01/24
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util.zip;
  24.  
  25. import java.io.InputStream;
  26. import java.io.IOException;
  27. import java.io.EOFException;
  28. import java.io.PushbackInputStream;
  29.  
  30. /**
  31.  * This class implements an input stream filter for reading files in the
  32.  * ZIP file format. Includes support for both compressed and uncompressed
  33.  * entries.
  34.  *
  35.  * @author    David Connelly
  36.  * @version    1.11, 01/24/97
  37.  */
  38. public
  39. class ZipInputStream extends InflaterInputStream implements ZipConstants {
  40.     private ZipEntry entry;
  41.     private CRC32 crc = new CRC32();
  42.     private long remaining;
  43.     private byte[] tmpbuf = new byte[512];
  44.  
  45.     private static final int STORED = ZipEntry.STORED;
  46.     private static final int DEFLATED = ZipEntry.DEFLATED;
  47.  
  48.     /**
  49.      * Creates a new ZIP input stream.
  50.      * @param in the actual input stream
  51.      */
  52.     public ZipInputStream(InputStream in) {
  53.     super(new PushbackInputStream(in, 512), new Inflater(true), 512);
  54.     }
  55.  
  56.     /**
  57.      * Reads the next ZIP file entry and positions stream at the beginning
  58.      * of the entry data.
  59.      * @exception ZipException if a ZIP file error has occurred
  60.      * @exception IOException if an I/O error has occurred
  61.      */
  62.     public ZipEntry getNextEntry() throws IOException {
  63.     if (entry != null) {
  64.         closeEntry();
  65.     }
  66.     crc.reset();
  67.     inf.reset();
  68.     if ((entry = readLOC()) == null) {
  69.         return null;
  70.     }
  71.     if (entry.method == STORED) {
  72.         remaining = entry.size;
  73.     }
  74.     return entry;
  75.     }
  76.  
  77.     /**
  78.      * Closes the current ZIP entry and positions the stream for reading the
  79.      * next entry.
  80.      * @exception ZipException if a ZIP file error has occurred
  81.      * @exception IOException if an I/O error has occurred
  82.      */
  83.     public void closeEntry() throws IOException {
  84.     while (read(tmpbuf, 0, tmpbuf.length) != -1) ;
  85.     }
  86.     
  87.     /**
  88.      * Reads from the current ZIP entry into an array of bytes. Blocks until
  89.      * some input is available.
  90.      * @param b the buffer into which the data is read
  91.      * @param off the start offset of the data
  92.      * @param len the maximum number of bytes read
  93.      * @return the actual number of bytes read, or -1 if the end of the
  94.      *         entry is reached
  95.      * @exception ZipException if a ZIP file error has occurred
  96.      * @exception IOException if an I/O error has occurred
  97.      */
  98.     public int read(byte[] b, int off, int len) throws IOException {
  99.     if (entry == null) {
  100.         return -1;
  101.     }
  102.     switch (entry.method) {
  103.     case DEFLATED:
  104.         len = super.read(b, off, len);
  105.         if (len == -1) {
  106.         readEnd(entry);
  107.         entry = null;
  108.         } else {
  109.         crc.update(b, off, len);
  110.         }
  111.         return len;
  112.     case STORED:
  113.         if (remaining <= 0) {
  114.         entry = null;
  115.         return -1;
  116.         }
  117.         if (len > remaining) {
  118.         len = (int)remaining;
  119.         }
  120.         len = in.read(b, off, len);
  121.         if (len == -1) {
  122.         throw new ZipException("unexpected EOF");
  123.         }
  124.         crc.update(b, off, len);
  125.         remaining -= len;
  126.         return len;
  127.     default:
  128.         throw new InternalError("invalid compression method");
  129.     }
  130.     }
  131.  
  132.     /**
  133.      * Skips specified number of bytes in the current ZIP entry.
  134.      * @param n the number of bytes to skip
  135.      * @return the actual number of bytes skipped
  136.      * @exception ZipException if a ZIP file error has occurred
  137.      * @exception IOException if an I/O error has occurred
  138.      */
  139.     public long skip(long n) throws IOException {
  140.     if (n <= 0) {
  141.         return 0;
  142.     }
  143.     n = Math.min(n, Integer.MAX_VALUE);
  144.     int total = 0;
  145.     while (total < n) {
  146.         int len = read(tmpbuf, 0, (int)n - total);
  147.         if (len == -1) {
  148.         break;
  149.         }
  150.         total += len;
  151.     }
  152.     return total;
  153.     }
  154.  
  155.     /**
  156.      * Closes the ZIP input stream.
  157.      * @exception IOException if an I/O error has occurred
  158.      */
  159.     public void close() throws IOException {
  160.     in.close();
  161.     }
  162.  
  163.     /*
  164.      * Reads local file (LOC) header for next entry.
  165.      */
  166.     private ZipEntry readLOC() throws IOException {
  167.     try {
  168.         readFully(tmpbuf, 0, LOCHDR);
  169.     } catch (EOFException e) {
  170.         return null;
  171.     }
  172.     if (get32(tmpbuf, 0) != LOCSIG) {
  173.         return null;
  174.     }
  175.     ZipEntry e = new ZipEntry();
  176.     e.version = get16(tmpbuf, LOCVER);
  177.     e.flag = get16(tmpbuf, LOCFLG);
  178.     if ((e.flag & 1) == 1) {
  179.         throw new ZipException("encrypted ZIP entry not supported");
  180.     }
  181.     e.method = get16(tmpbuf, LOCHOW);
  182.     e.time = get32(tmpbuf, LOCTIM);
  183.     if ((e.flag & 8) == 8) {
  184.         /* EXT descriptor present */
  185.         if (e.method != DEFLATED) {
  186.         throw new ZipException(
  187.             "only DEFLATED entries can have EXT descriptor");
  188.         }
  189.     } else {
  190.         e.crc = get32(tmpbuf, LOCCRC);
  191.         e.csize = get32(tmpbuf, LOCSIZ);
  192.         e.size = get32(tmpbuf, LOCLEN);
  193.     }
  194.     int len = get16(tmpbuf, LOCNAM);
  195.     if (len == 0) {
  196.         throw new ZipException("missing entry name");
  197.     }
  198.     byte[] b = new byte[len];
  199.     readFully(b, 0, len);
  200.     e.name = new String(b, 0, 0, len);
  201.     len = get16(tmpbuf, LOCEXT);
  202.     if (len > 0) {
  203.         b = new byte[len];
  204.         readFully(b, 0, len);
  205.         e.extra = b;
  206.     }
  207.     return e;
  208.     }
  209.  
  210.     /*
  211.      * Reads end of deflated entry as well as EXT descriptor if present.
  212.      */
  213.     private void readEnd(ZipEntry e) throws IOException {
  214.     int n = inf.getRemaining();
  215.     if (n > 0) {
  216.         ((PushbackInputStream)in).unread(buf, len - n, n);
  217.     }
  218.     if ((e.flag & 8) == 8) {
  219.         /* EXT descriptor present */
  220.         readFully(tmpbuf, 0, EXTHDR);
  221.         long sig = get32(tmpbuf, 0);
  222.         if (sig != EXTSIG) {
  223.         throw new ZipException("invalid EXT descriptor signature");
  224.         }
  225.         e.crc = get32(tmpbuf, EXTCRC);
  226.         e.csize = get32(tmpbuf, EXTSIZ);
  227.         e.size = get32(tmpbuf, EXTLEN);
  228.     }
  229.     if (e.size != inf.getTotalOut()) {
  230.         throw new ZipException(
  231.         "invalid entry size (expected " + e.size + " but got " +
  232.         inf.getTotalOut() + " bytes)");
  233.     }
  234.     if (e.csize != inf.getTotalIn()) {
  235.         throw new ZipException(
  236.         "invalid entry compressed size (expected " + e.csize +
  237.         " but got " + inf.getTotalIn() + " bytes)");
  238.     }
  239.     if (e.crc != crc.getValue()) {
  240.         throw new ZipException(
  241.         "invalid entry CRC (expected 0x" + Long.toHexString(e.crc) +
  242.         " but got 0x" + Long.toHexString(crc.getValue()) + ")");
  243.     }
  244.     }
  245.  
  246.     /*
  247.      * Reads bytes, blocking until all bytes are read.
  248.      */
  249.     private void readFully(byte[] b, int off, int len) throws IOException {
  250.     while (len > 0) {
  251.         int n = in.read(b, off, len);
  252.         if (n == -1) {
  253.         throw new EOFException();
  254.         }
  255.         off += n;
  256.         len -= n;
  257.     }
  258.     }
  259.  
  260.     /*
  261.      * Fetches unsigned 16-bit value from byte array at specified offset.
  262.      * The bytes are assumed to be in Intel (little-endian) byte order.
  263.      */
  264.     private static final int get16(byte b[], int off) {
  265.     return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
  266.     }
  267.  
  268.     /*
  269.      * Fetches unsigned 32-bit value from byte array at specified offset.
  270.      * The bytes are assumed to be in Intel (little-endian) byte order.
  271.      */
  272.     private static final long get32(byte b[], int off) {
  273.     return get16(b, off) | ((long)get16(b, off+2) << 16);
  274.     }
  275. }
  276.