home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / java / util / zip / ZipInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  4.4 KB  |  202 lines

  1. package java.util.zip;
  2.  
  3. import java.io.EOFException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.PushbackInputStream;
  7.  
  8. public class ZipInputStream extends InflaterInputStream implements ZipConstants {
  9.    private ZipEntry entry;
  10.    private CRC32 crc = new CRC32();
  11.    private long remaining;
  12.    private byte[] tmpbuf = new byte[512];
  13.    private static final int STORED = 0;
  14.    private static final int DEFLATED = 8;
  15.  
  16.    public ZipInputStream(InputStream var1) {
  17.       super(new PushbackInputStream(var1, 512), new Inflater(true), 512);
  18.    }
  19.  
  20.    public ZipEntry getNextEntry() throws IOException {
  21.       if (this.entry != null) {
  22.          this.closeEntry();
  23.       }
  24.  
  25.       this.crc.reset();
  26.       super.inf.reset();
  27.       if ((this.entry = this.readLOC()) == null) {
  28.          return null;
  29.       } else {
  30.          if (this.entry.method == 0) {
  31.             this.remaining = this.entry.size;
  32.          }
  33.  
  34.          return this.entry;
  35.       }
  36.    }
  37.  
  38.    public void closeEntry() throws IOException {
  39.       while(this.read(this.tmpbuf, 0, this.tmpbuf.length) != -1) {
  40.       }
  41.  
  42.    }
  43.  
  44.    public int read(byte[] var1, int var2, int var3) throws IOException {
  45.       if (this.entry == null) {
  46.          return -1;
  47.       } else {
  48.          switch (this.entry.method) {
  49.             case 0:
  50.                if (this.remaining <= 0L) {
  51.                   this.entry = null;
  52.                   return -1;
  53.                } else {
  54.                   if ((long)var3 > this.remaining) {
  55.                      var3 = (int)this.remaining;
  56.                   }
  57.  
  58.                   var3 = super.in.read(var1, var2, var3);
  59.                   if (var3 == -1) {
  60.                      throw new ZipException("unexpected EOF");
  61.                   }
  62.  
  63.                   this.crc.update(var1, var2, var3);
  64.                   this.remaining -= (long)var3;
  65.                   return var3;
  66.                }
  67.             case 8:
  68.                var3 = super.read(var1, var2, var3);
  69.                if (var3 == -1) {
  70.                   this.readEnd(this.entry);
  71.                   this.entry = null;
  72.                } else {
  73.                   this.crc.update(var1, var2, var3);
  74.                }
  75.  
  76.                return var3;
  77.             default:
  78.                throw new InternalError("invalid compression method");
  79.          }
  80.       }
  81.    }
  82.  
  83.    public long skip(long var1) throws IOException {
  84.       if (var1 <= 0L) {
  85.          return 0L;
  86.       } else {
  87.          var1 = Math.min(var1, 2147483647L);
  88.  
  89.          int var3;
  90.          int var4;
  91.          for(var3 = 0; (long)var3 < var1; var3 += var4) {
  92.             var4 = this.read(this.tmpbuf, 0, (int)var1 - var3);
  93.             if (var4 == -1) {
  94.                break;
  95.             }
  96.          }
  97.  
  98.          return (long)var3;
  99.       }
  100.    }
  101.  
  102.    public void close() throws IOException {
  103.       super.in.close();
  104.    }
  105.  
  106.    private ZipEntry readLOC() throws IOException {
  107.       try {
  108.          this.readFully(this.tmpbuf, 0, 30);
  109.       } catch (EOFException var4) {
  110.          return null;
  111.       }
  112.  
  113.       if (get32(this.tmpbuf, 0) != 67324752L) {
  114.          return null;
  115.       } else {
  116.          ZipEntry var1 = new ZipEntry();
  117.          var1.version = get16(this.tmpbuf, 4);
  118.          var1.flag = get16(this.tmpbuf, 6);
  119.          if ((var1.flag & 1) == 1) {
  120.             throw new ZipException("encrypted ZIP entry not supported");
  121.          } else {
  122.             var1.method = get16(this.tmpbuf, 8);
  123.             var1.time = get32(this.tmpbuf, 10);
  124.             if ((var1.flag & 8) == 8) {
  125.                if (var1.method != 8) {
  126.                   throw new ZipException("only DEFLATED entries can have EXT descriptor");
  127.                }
  128.             } else {
  129.                var1.crc = get32(this.tmpbuf, 14);
  130.                var1.csize = get32(this.tmpbuf, 18);
  131.                var1.size = get32(this.tmpbuf, 22);
  132.             }
  133.  
  134.             int var2 = get16(this.tmpbuf, 26);
  135.             if (var2 == 0) {
  136.                throw new ZipException("missing entry name");
  137.             } else {
  138.                byte[] var3 = new byte[var2];
  139.                this.readFully(var3, 0, var2);
  140.                var1.name = new String(var3, 0, 0, var2);
  141.                var2 = get16(this.tmpbuf, 28);
  142.                if (var2 > 0) {
  143.                   var3 = new byte[var2];
  144.                   this.readFully(var3, 0, var2);
  145.                   var1.extra = var3;
  146.                }
  147.  
  148.                return var1;
  149.             }
  150.          }
  151.       }
  152.    }
  153.  
  154.    private void readEnd(ZipEntry var1) throws IOException {
  155.       int var2 = super.inf.getRemaining();
  156.       if (var2 > 0) {
  157.          ((PushbackInputStream)super.in).unread(super.buf, super.len - var2, var2);
  158.       }
  159.  
  160.       if ((var1.flag & 8) == 8) {
  161.          this.readFully(this.tmpbuf, 0, 16);
  162.          long var3 = get32(this.tmpbuf, 0);
  163.          if (var3 != 134695760L) {
  164.             throw new ZipException("invalid EXT descriptor signature");
  165.          }
  166.  
  167.          var1.crc = get32(this.tmpbuf, 4);
  168.          var1.csize = get32(this.tmpbuf, 8);
  169.          var1.size = get32(this.tmpbuf, 12);
  170.       }
  171.  
  172.       if (var1.size != (long)super.inf.getTotalOut()) {
  173.          throw new ZipException("invalid entry size (expected " + var1.size + " but got " + super.inf.getTotalOut() + " bytes)");
  174.       } else if (var1.csize != (long)super.inf.getTotalIn()) {
  175.          throw new ZipException("invalid entry compressed size (expected " + var1.csize + " but got " + super.inf.getTotalIn() + " bytes)");
  176.       } else if (var1.crc != this.crc.getValue()) {
  177.          throw new ZipException("invalid entry CRC (expected 0x" + Long.toHexString(var1.crc) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
  178.       }
  179.    }
  180.  
  181.    private void readFully(byte[] var1, int var2, int var3) throws IOException {
  182.       while(var3 > 0) {
  183.          int var4 = super.in.read(var1, var2, var3);
  184.          if (var4 == -1) {
  185.             throw new EOFException();
  186.          }
  187.  
  188.          var2 += var4;
  189.          var3 -= var4;
  190.       }
  191.  
  192.    }
  193.  
  194.    private static final int get16(byte[] var0, int var1) {
  195.       return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
  196.    }
  197.  
  198.    private static final long get32(byte[] var0, int var1) {
  199.       return (long)get16(var0, var1) | (long)get16(var0, var1 + 2) << 16;
  200.    }
  201. }
  202.