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 / ZipFile.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  4.5 KB  |  217 lines

  1. package java.util.zip;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.RandomAccessFile;
  7. import java.util.Enumeration;
  8. import java.util.Hashtable;
  9.  
  10. public class ZipFile implements ZipConstants {
  11.    RandomAccessFile raf;
  12.    private String name;
  13.    private Hashtable entries;
  14.    long cenpos;
  15.    private long endpos;
  16.    long pos;
  17.    private static final int STORED = 0;
  18.    private static final int DEFLATED = 8;
  19.    private static final int INBUFSIZ = 64;
  20.  
  21.    public ZipFile(String var1) throws IOException {
  22.       this.raf = new RandomAccessFile(var1, "r");
  23.       this.name = var1;
  24.       this.readCEN();
  25.    }
  26.  
  27.    public ZipFile(File var1) throws ZipException, IOException {
  28.       this(var1.getPath());
  29.    }
  30.  
  31.    public ZipEntry getEntry(String var1) {
  32.       return (ZipEntry)this.entries.get(var1);
  33.    }
  34.  
  35.    public InputStream getInputStream(ZipEntry var1) throws IOException {
  36.       ZipFileInputStream var2 = new ZipFileInputStream(this, var1);
  37.       switch (var1.method) {
  38.          case 0:
  39.             return var2;
  40.          case 8:
  41.             return new InflaterInputStream(var2, new Inflater(true));
  42.          default:
  43.             throw new ZipException("invalid compression method");
  44.       }
  45.    }
  46.  
  47.    public String getName() {
  48.       return this.name;
  49.    }
  50.  
  51.    public Enumeration entries() {
  52.       return this.entries.elements();
  53.    }
  54.  
  55.    public void close() throws IOException {
  56.       if (this.raf != null) {
  57.          this.raf.close();
  58.          this.raf = null;
  59.       }
  60.  
  61.    }
  62.  
  63.    synchronized int read(long var1, byte[] var3, int var4, int var5) throws IOException {
  64.       if (var1 != this.pos) {
  65.          this.raf.seek(var1);
  66.       }
  67.  
  68.       int var6 = this.raf.read(var3, var4, var5);
  69.       if (var6 > 0) {
  70.          this.pos = var1 + (long)var6;
  71.       }
  72.  
  73.       return var6;
  74.    }
  75.  
  76.    synchronized int read(long var1) throws IOException {
  77.       if (var1 != this.pos) {
  78.          this.raf.seek(var1);
  79.       }
  80.  
  81.       int var3 = this.raf.read();
  82.       if (var3 > 0) {
  83.          this.pos = var1 + 1L;
  84.       }
  85.  
  86.       return var3;
  87.    }
  88.  
  89.    private void readCEN() throws IOException {
  90.       this.findEND();
  91.       byte[] var1 = new byte[22];
  92.       this.raf.readFully(var1);
  93.       if (get32(var1, 0) != 101010256L) {
  94.          throw new ZipException("invalid END header signature");
  95.       } else {
  96.          this.cenpos = get32(var1, 16);
  97.          int var2 = (int)get32(var1, 12);
  98.          if (this.cenpos + (long)var2 != this.endpos) {
  99.             throw new ZipException("invalid END header format");
  100.          } else {
  101.             int var3 = get16(var1, 10);
  102.             if (var3 * 46 > var2) {
  103.                throw new ZipException("invalid END header format");
  104.             } else if (get16(var1, 8) != var3) {
  105.                throw new ZipException("cannot have more than one drive");
  106.             } else {
  107.                this.raf.seek(this.cenpos);
  108.                byte[] var4 = new byte[var2];
  109.                this.raf.readFully(var4);
  110.                this.entries = new Hashtable(var3);
  111.  
  112.                ZipEntry var6;
  113.                for(int var5 = 0; var5 < var2; this.entries.put(var6.name, var6)) {
  114.                   if (get32(var4, var5) != 33639248L) {
  115.                      throw new ZipException("invalid CEN header signature");
  116.                   }
  117.  
  118.                   var6 = new ZipEntry();
  119.                   var6.version = get16(var4, var5 + 6);
  120.                   var6.flag = get16(var4, var5 + 8);
  121.                   var6.method = get16(var4, var5 + 10);
  122.                   var6.time = get32(var4, var5 + 12);
  123.                   var6.crc = get32(var4, var5 + 16);
  124.                   var6.size = get32(var4, var5 + 24);
  125.                   var6.csize = get32(var4, var5 + 20);
  126.                   var6.offset = get32(var4, var5 + 42);
  127.                   if (var6.offset + var6.csize > this.cenpos) {
  128.                      throw new ZipException("invalid CEN entry size");
  129.                   }
  130.  
  131.                   int var7 = var5;
  132.                   var5 += 46;
  133.                   int var8 = get16(var4, var7 + 28);
  134.                   if (var8 == 0 || var5 + var8 > var2) {
  135.                      throw new ZipException("invalid CEN entry name");
  136.                   }
  137.  
  138.                   var6.name = new String(var4, 0, var5, var8);
  139.                   var5 += var8;
  140.                   var8 = get16(var4, var7 + 30);
  141.                   if (var8 > 0) {
  142.                      if (var5 + var8 > var2) {
  143.                         throw new ZipException("invalid CEN entry extra data");
  144.                      }
  145.  
  146.                      var6.extra = new byte[var8];
  147.                      System.arraycopy(var4, var5, var6.extra, 0, var8);
  148.                      var5 += var8;
  149.                   }
  150.  
  151.                   var8 = get16(var4, var7 + 32);
  152.                   if (var8 > 0) {
  153.                      if (var5 + var8 > var2) {
  154.                         throw new ZipException("invalid CEN entry comment");
  155.                      }
  156.  
  157.                      var6.comment = new String(var4, 0, var5, var8);
  158.                      var5 += var8;
  159.                   }
  160.                }
  161.  
  162.                if (this.entries.size() != var3) {
  163.                   throw new ZipException("invalid CEN header format");
  164.                }
  165.             }
  166.          }
  167.       }
  168.    }
  169.  
  170.    private void findEND() throws IOException {
  171.       long var1 = this.raf.length();
  172.       this.raf.seek(var1);
  173.       long var3 = Math.max(0L, var1 - 65535L);
  174.       byte[] var5 = new byte[68];
  175.       this.pos = var1;
  176.  
  177.       while(this.pos > var3) {
  178.          int var6 = Math.min((int)(this.pos - var3), 64);
  179.          this.pos -= (long)var6;
  180.          this.raf.seek(this.pos);
  181.          this.raf.readFully(var5, 0, var6);
  182.  
  183.          while(true) {
  184.             --var6;
  185.             if (var6 <= 0) {
  186.                break;
  187.             }
  188.  
  189.             if (get32(var5, var6) == 101010256L) {
  190.                this.endpos = this.pos + (long)var6;
  191.                if (var1 - this.endpos >= 22L) {
  192.                   this.raf.seek(this.endpos);
  193.                   byte[] var7 = new byte[22];
  194.                   this.raf.readFully(var7);
  195.                   int var8 = get16(var7, 20);
  196.                   if (this.endpos + 22L + (long)var8 == var1) {
  197.                      this.raf.seek(this.endpos);
  198.                      this.pos = this.endpos;
  199.                      return;
  200.                   }
  201.                }
  202.             }
  203.          }
  204.       }
  205.  
  206.       throw new ZipException("not a ZIP file (END header not found)");
  207.    }
  208.  
  209.    static final int get16(byte[] var0, int var1) {
  210.       return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
  211.    }
  212.  
  213.    static final long get32(byte[] var0, int var1) {
  214.       return (long)get16(var0, var1) | (long)get16(var0, var1 + 2) << 16;
  215.    }
  216. }
  217.