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;
- private boolean closed = false;
- private boolean entryEOF = false;
-
- private void ensureOpen() throws IOException {
- if (this.closed) {
- throw new IOException("Stream closed");
- }
- }
-
- public ZipInputStream(InputStream var1) {
- super(new PushbackInputStream(var1, 512), new Inflater(true), 512);
- if (var1 == null) {
- throw new NullPointerException("in is null");
- }
- }
-
- public ZipEntry getNextEntry() throws IOException {
- this.ensureOpen();
- 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;
- }
-
- this.entryEOF = false;
- return this.entry;
- }
- }
-
- public void closeEntry() throws IOException {
- this.ensureOpen();
-
- while(this.read(this.tmpbuf, 0, this.tmpbuf.length) != -1) {
- }
-
- this.entryEOF = true;
- }
-
- public int available() throws IOException {
- this.ensureOpen();
- return this.entryEOF ? 0 : 1;
- }
-
- public int read(byte[] var1, int var2, int var3) throws IOException {
- this.ensureOpen();
- if (var2 >= 0 && var2 <= var1.length && var3 >= 0 && var2 + var3 <= var1.length && var2 + var3 >= 0) {
- if (var3 == 0) {
- return 0;
- } else if (this.entry == null) {
- return -1;
- } else {
- switch (this.entry.method) {
- case 0:
- if (this.remaining <= 0L) {
- this.entryEOF = true;
- 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.entryEOF = true;
- this.entry = null;
- } else {
- this.crc.update(var1, var2, var3);
- }
-
- return var3;
- default:
- throw new InternalError("invalid compression method");
- }
- }
- } else {
- throw new IndexOutOfBoundsException();
- }
- }
-
- public long skip(long var1) throws IOException {
- if (var1 < 0L) {
- throw new IllegalArgumentException("negative skip length");
- } else {
- this.ensureOpen();
- int var3 = (int)Math.min(var1, 2147483647L);
-
- int var4;
- int var6;
- for(var4 = 0; var4 < var3; var4 += var6) {
- var6 = var3 - var4;
- if (var6 > this.tmpbuf.length) {
- var6 = this.tmpbuf.length;
- }
-
- var6 = this.read(this.tmpbuf, 0, var6);
- if (var6 == -1) {
- this.entryEOF = true;
- break;
- }
- }
-
- return (long)var4;
- }
- }
-
- public void close() throws IOException {
- super.close();
- this.closed = true;
- }
-
- 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 {
- int var1 = get16(this.tmpbuf, 26);
- if (var1 == 0) {
- throw new ZipException("missing entry name");
- } else {
- byte[] var2 = new byte[var1];
- this.readFully(var2, 0, var1);
- ZipEntry var3 = this.createZipEntry(getUTF8String(var2, 0, var1));
- var3.version = get16(this.tmpbuf, 4);
- var3.flag = get16(this.tmpbuf, 6);
- if ((var3.flag & 1) == 1) {
- throw new ZipException("encrypted ZIP entry not supported");
- } else {
- var3.method = get16(this.tmpbuf, 8);
- var3.time = get32(this.tmpbuf, 10);
- if ((var3.flag & 8) == 8) {
- if (var3.method != 8) {
- throw new ZipException("only DEFLATED entries can have EXT descriptor");
- }
- } else {
- var3.crc = get32(this.tmpbuf, 14);
- var3.csize = get32(this.tmpbuf, 18);
- var3.size = get32(this.tmpbuf, 22);
- }
-
- var1 = get16(this.tmpbuf, 28);
- if (var1 > 0) {
- var2 = new byte[var1];
- this.readFully(var2, 0, var1);
- var3.extra = var2;
- }
-
- return var3;
- }
- }
- }
- }
-
- private static String getUTF8String(byte[] var0, int var1, int var2) {
- int var3 = 0;
- int var4 = var1 + var2;
- int var5 = var1;
-
- while(var5 < var4) {
- int var6 = var0[var5++] & 255;
- switch (var6 >> 4) {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- ++var3;
- break;
- case 8:
- case 9:
- case 10:
- case 11:
- default:
- throw new IllegalArgumentException();
- case 12:
- case 13:
- if ((var0[var5++] & 192) != 128) {
- throw new IllegalArgumentException();
- }
-
- ++var3;
- break;
- case 14:
- if ((var0[var5++] & 192) != 128 || (var0[var5++] & 192) != 128) {
- throw new IllegalArgumentException();
- }
-
- ++var3;
- }
- }
-
- if (var5 != var4) {
- throw new IllegalArgumentException();
- } else {
- char[] var12 = new char[var3];
- var5 = 0;
-
- while(var1 < var4) {
- int var7 = var0[var1++] & 255;
- switch (var7 >> 4) {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- var12[var5++] = (char)var7;
- break;
- case 8:
- case 9:
- case 10:
- case 11:
- default:
- throw new IllegalArgumentException();
- case 12:
- case 13:
- var12[var5++] = (char)((var7 & 31) << 6 | var0[var1++] & 63);
- break;
- case 14:
- int var8 = (var0[var1++] & 63) << 6;
- var12[var5++] = (char)((var7 & 15) << 12 | var8 | var0[var1++] & 63);
- }
- }
-
- return new String(var12, 0, var3);
- }
- }
-
- protected ZipEntry createZipEntry(String var1) {
- return new ZipEntry(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;
- }
- }
-