home *** CD-ROM | disk | FTP | other *** search
- package java.util.zip;
-
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.util.Enumeration;
- import java.util.Hashtable;
-
- public class ZipFile implements ZipConstants {
- RandomAccessFile raf;
- private String name;
- private Hashtable entries;
- long cenpos;
- private long endpos;
- long pos;
- private static final int STORED = 0;
- private static final int DEFLATED = 8;
- private static final int INBUFSIZ = 64;
-
- public ZipFile(String var1) throws IOException {
- this.raf = new RandomAccessFile(var1, "r");
- this.name = var1;
- this.readCEN();
- }
-
- public ZipFile(File var1) throws ZipException, IOException {
- this(var1.getPath());
- }
-
- public ZipEntry getEntry(String var1) {
- return (ZipEntry)this.entries.get(var1);
- }
-
- public InputStream getInputStream(ZipEntry var1) throws IOException {
- ZipFileInputStream var2 = new ZipFileInputStream(this, var1);
- switch (var1.method) {
- case 0:
- return var2;
- case 8:
- return new InflaterInputStream(var2, new Inflater(true));
- default:
- throw new ZipException("invalid compression method");
- }
- }
-
- public String getName() {
- return this.name;
- }
-
- public Enumeration entries() {
- return this.entries.elements();
- }
-
- public void close() throws IOException {
- if (this.raf != null) {
- this.raf.close();
- this.raf = null;
- }
-
- }
-
- synchronized int read(long var1, byte[] var3, int var4, int var5) throws IOException {
- if (var1 != this.pos) {
- this.raf.seek(var1);
- }
-
- int var6 = this.raf.read(var3, var4, var5);
- if (var6 > 0) {
- this.pos = var1 + (long)var6;
- }
-
- return var6;
- }
-
- synchronized int read(long var1) throws IOException {
- if (var1 != this.pos) {
- this.raf.seek(var1);
- }
-
- int var3 = this.raf.read();
- if (var3 > 0) {
- this.pos = var1 + 1L;
- }
-
- return var3;
- }
-
- private void readCEN() throws IOException {
- this.findEND();
- byte[] var1 = new byte[22];
- this.raf.readFully(var1);
- if (get32(var1, 0) != 101010256L) {
- throw new ZipException("invalid END header signature");
- } else {
- this.cenpos = get32(var1, 16);
- int var2 = (int)get32(var1, 12);
- if (this.cenpos + (long)var2 != this.endpos) {
- throw new ZipException("invalid END header format");
- } else {
- int var3 = get16(var1, 10);
- if (var3 * 46 > var2) {
- throw new ZipException("invalid END header format");
- } else if (get16(var1, 8) != var3) {
- throw new ZipException("cannot have more than one drive");
- } else {
- this.raf.seek(this.cenpos);
- byte[] var4 = new byte[var2];
- this.raf.readFully(var4);
- this.entries = new Hashtable(var3);
-
- ZipEntry var6;
- for(int var5 = 0; var5 < var2; this.entries.put(var6.name, var6)) {
- if (get32(var4, var5) != 33639248L) {
- throw new ZipException("invalid CEN header signature");
- }
-
- var6 = new ZipEntry();
- var6.version = get16(var4, var5 + 6);
- var6.flag = get16(var4, var5 + 8);
- var6.method = get16(var4, var5 + 10);
- var6.time = get32(var4, var5 + 12);
- var6.crc = get32(var4, var5 + 16);
- var6.size = get32(var4, var5 + 24);
- var6.csize = get32(var4, var5 + 20);
- var6.offset = get32(var4, var5 + 42);
- if (var6.offset + var6.csize > this.cenpos) {
- throw new ZipException("invalid CEN entry size");
- }
-
- int var7 = var5;
- var5 += 46;
- int var8 = get16(var4, var7 + 28);
- if (var8 == 0 || var5 + var8 > var2) {
- throw new ZipException("invalid CEN entry name");
- }
-
- var6.name = new String(var4, 0, var5, var8);
- var5 += var8;
- var8 = get16(var4, var7 + 30);
- if (var8 > 0) {
- if (var5 + var8 > var2) {
- throw new ZipException("invalid CEN entry extra data");
- }
-
- var6.extra = new byte[var8];
- System.arraycopy(var4, var5, var6.extra, 0, var8);
- var5 += var8;
- }
-
- var8 = get16(var4, var7 + 32);
- if (var8 > 0) {
- if (var5 + var8 > var2) {
- throw new ZipException("invalid CEN entry comment");
- }
-
- var6.comment = new String(var4, 0, var5, var8);
- var5 += var8;
- }
- }
-
- if (this.entries.size() != var3) {
- throw new ZipException("invalid CEN header format");
- }
- }
- }
- }
- }
-
- private void findEND() throws IOException {
- long var1 = this.raf.length();
- this.raf.seek(var1);
- long var3 = Math.max(0L, var1 - 65535L);
- byte[] var5 = new byte[68];
- this.pos = var1;
-
- while(this.pos > var3) {
- int var6 = Math.min((int)(this.pos - var3), 64);
- this.pos -= (long)var6;
- this.raf.seek(this.pos);
- this.raf.readFully(var5, 0, var6);
-
- while(true) {
- --var6;
- if (var6 <= 0) {
- break;
- }
-
- if (get32(var5, var6) == 101010256L) {
- this.endpos = this.pos + (long)var6;
- if (var1 - this.endpos >= 22L) {
- this.raf.seek(this.endpos);
- byte[] var7 = new byte[22];
- this.raf.readFully(var7);
- int var8 = get16(var7, 20);
- if (this.endpos + 22L + (long)var8 == var1) {
- this.raf.seek(this.endpos);
- this.pos = this.endpos;
- return;
- }
- }
- }
- }
- }
-
- throw new ZipException("not a ZIP file (END header not found)");
- }
-
- static final int get16(byte[] var0, int var1) {
- return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
- }
-
- static final long get32(byte[] var0, int var1) {
- return (long)get16(var0, var1) | (long)get16(var0, var1 + 2) << 16;
- }
- }
-