home *** CD-ROM | disk | FTP | other *** search
- package java.util.zip;
-
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PushbackInputStream;
-
- public class ZipInputStream extends InflaterInputStream implements ZipConstants {
- private ZipEntry entry;
- private CRC32 crc = new CRC32();
- private long remaining;
- private byte[] tmpbuf = new byte[512];
- private static final int STORED = 0;
- private static final int DEFLATED = 8;
-
- public ZipInputStream(InputStream var1) {
- super(new PushbackInputStream(var1, 512), new Inflater(true), 512);
- }
-
- public ZipEntry getNextEntry() throws IOException {
- if (this.entry != null) {
- this.closeEntry();
- }
-
- this.crc.reset();
- super.inf.reset();
- if ((this.entry = this.readLOC()) == null) {
- return null;
- } else {
- if (this.entry.method == 0) {
- this.remaining = this.entry.size;
- }
-
- return this.entry;
- }
- }
-
- public void closeEntry() throws IOException {
- while(this.read(this.tmpbuf, 0, this.tmpbuf.length) != -1) {
- }
-
- }
-
- public int read(byte[] var1, int var2, int var3) throws IOException {
- if (this.entry == null) {
- return -1;
- } else {
- switch (this.entry.method) {
- case 0:
- if (this.remaining <= 0L) {
- this.entry = null;
- return -1;
- } else {
- if ((long)var3 > this.remaining) {
- var3 = (int)this.remaining;
- }
-
- var3 = super.in.read(var1, var2, var3);
- if (var3 == -1) {
- throw new ZipException("unexpected EOF");
- }
-
- this.crc.update(var1, var2, var3);
- this.remaining -= (long)var3;
- return var3;
- }
- case 8:
- var3 = super.read(var1, var2, var3);
- if (var3 == -1) {
- this.readEnd(this.entry);
- this.entry = null;
- } else {
- this.crc.update(var1, var2, var3);
- }
-
- return var3;
- default:
- throw new InternalError("invalid compression method");
- }
- }
- }
-
- public long skip(long var1) throws IOException {
- if (var1 <= 0L) {
- return 0L;
- } else {
- var1 = Math.min(var1, 2147483647L);
-
- int var3;
- int var4;
- for(var3 = 0; (long)var3 < var1; var3 += var4) {
- var4 = this.read(this.tmpbuf, 0, (int)var1 - var3);
- if (var4 == -1) {
- break;
- }
- }
-
- return (long)var3;
- }
- }
-
- public void close() throws IOException {
- super.in.close();
- }
-
- private ZipEntry readLOC() throws IOException {
- try {
- this.readFully(this.tmpbuf, 0, 30);
- } catch (EOFException var4) {
- return null;
- }
-
- if (get32(this.tmpbuf, 0) != 67324752L) {
- return null;
- } else {
- ZipEntry var1 = new ZipEntry();
- var1.version = get16(this.tmpbuf, 4);
- var1.flag = get16(this.tmpbuf, 6);
- if ((var1.flag & 1) == 1) {
- throw new ZipException("encrypted ZIP entry not supported");
- } else {
- var1.method = get16(this.tmpbuf, 8);
- var1.time = get32(this.tmpbuf, 10);
- if ((var1.flag & 8) == 8) {
- if (var1.method != 8) {
- throw new ZipException("only DEFLATED entries can have EXT descriptor");
- }
- } else {
- var1.crc = get32(this.tmpbuf, 14);
- var1.csize = get32(this.tmpbuf, 18);
- var1.size = get32(this.tmpbuf, 22);
- }
-
- int var2 = get16(this.tmpbuf, 26);
- if (var2 == 0) {
- throw new ZipException("missing entry name");
- } else {
- byte[] var3 = new byte[var2];
- this.readFully(var3, 0, var2);
- var1.name = new String(var3, 0, 0, var2);
- var2 = get16(this.tmpbuf, 28);
- if (var2 > 0) {
- var3 = new byte[var2];
- this.readFully(var3, 0, var2);
- var1.extra = var3;
- }
-
- return var1;
- }
- }
- }
- }
-
- private void readEnd(ZipEntry var1) throws IOException {
- int var2 = super.inf.getRemaining();
- if (var2 > 0) {
- ((PushbackInputStream)super.in).unread(super.buf, super.len - var2, var2);
- }
-
- if ((var1.flag & 8) == 8) {
- this.readFully(this.tmpbuf, 0, 16);
- long var3 = get32(this.tmpbuf, 0);
- if (var3 != 134695760L) {
- throw new ZipException("invalid EXT descriptor signature");
- }
-
- var1.crc = get32(this.tmpbuf, 4);
- var1.csize = get32(this.tmpbuf, 8);
- var1.size = get32(this.tmpbuf, 12);
- }
-
- if (var1.size != (long)super.inf.getTotalOut()) {
- throw new ZipException("invalid entry size (expected " + var1.size + " but got " + super.inf.getTotalOut() + " bytes)");
- } else if (var1.csize != (long)super.inf.getTotalIn()) {
- throw new ZipException("invalid entry compressed size (expected " + var1.csize + " but got " + super.inf.getTotalIn() + " bytes)");
- } else if (var1.crc != this.crc.getValue()) {
- throw new ZipException("invalid entry CRC (expected 0x" + Long.toHexString(var1.crc) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
- }
- }
-
- private void readFully(byte[] var1, int var2, int var3) throws IOException {
- while(var3 > 0) {
- int var4 = super.in.read(var1, var2, var3);
- if (var4 == -1) {
- throw new EOFException();
- }
-
- var2 += var4;
- var3 -= var4;
- }
-
- }
-
- private static final int get16(byte[] var0, int var1) {
- return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
- }
-
- private static final long get32(byte[] var0, int var1) {
- return (long)get16(var0, var1) | (long)get16(var0, var1 + 2) << 16;
- }
- }
-