Changeset 7460


Ignore:
Timestamp:
12/24/10 14:50:16 (9 years ago)
Author:
melissa
Message:

Updated ZipHandle and ZipReader to use a ZipInputStream instead of a ZipFile. Closes #608.

Location:
trunk/components
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/bio-formats/src/loci/formats/in/ZipReader.java

    r6881 r7460  
    2727import java.util.Enumeration; 
    2828import java.util.zip.ZipEntry; 
    29 import java.util.zip.ZipFile; 
     29import java.util.zip.ZipInputStream; 
    3030 
    3131import loci.common.Location; 
     32import loci.common.RandomAccessInputStream; 
    3233import loci.common.ZipHandle; 
    3334import loci.formats.FormatException; 
     
    101102    reader.setMetadataStore(getMetadataStore()); 
    102103 
    103     ZipFile zip = new ZipFile(id); 
    104     Enumeration<? extends ZipEntry> e = zip.entries(); 
    105     while (e.hasMoreElements()) { 
    106       ZipEntry ze = e.nextElement(); 
     104    in = new RandomAccessInputStream(id); 
     105 
     106    ZipInputStream zip = new ZipInputStream(in); 
     107    while (true) { 
     108      ZipEntry ze = zip.getNextEntry(); 
     109      if (ze == null) break; 
    107110      ZipHandle handle = new ZipHandle(id, ze); 
    108111      Location.mapFile(ze.getName(), handle); 
  • trunk/components/common/src/loci/common/ZipHandle.java

    r6881 r7460  
    3131import java.util.Enumeration; 
    3232import java.util.zip.ZipEntry; 
    33 import java.util.zip.ZipFile; 
     33import java.util.zip.ZipInputStream; 
    3434 
    3535/** 
     
    4949  // -- Fields -- 
    5050 
    51   private ZipFile zip; 
     51  private RandomAccessInputStream in; 
     52  private ZipInputStream zip; 
    5253  private ZipEntry entry; 
     54  private int entryCount = 0; 
     55  private String file; 
    5356 
    5457  // -- Constructor -- 
     
    5659  public ZipHandle(String file) throws IOException { 
    5760    super(); 
    58     zip = new ZipFile(file); 
     61    this.file = file; 
     62    in = new RandomAccessInputStream(getHandle(file)); 
     63    zip = new ZipInputStream(in); 
    5964 
    6065    // strip off .zip extension and directory prefix 
     
    6671    // look for Zip entry with same prefix as the original Zip file 
    6772    entry = null; 
    68     Enumeration<? extends ZipEntry> e = zip.entries(); 
    69     while (e.hasMoreElements()) { 
    70       ZipEntry ze = e.nextElement(); 
     73 
     74    while (true) { 
     75      ZipEntry ze = zip.getNextEntry(); 
     76      if (ze == null) break; 
     77      entryCount++; 
     78    } 
     79    resetStream(); 
     80 
     81    while (true) { 
     82      ZipEntry ze = zip.getNextEntry(); 
     83      if (ze == null) break; 
     84      if (entry == null) entry = ze; 
    7185      if (ze.getName().startsWith(innerFile)) { 
    7286        // found entry with matching name 
     
    7589      } 
    7690    } 
    77     if (entry == null) { 
    78       entry = zip.entries().nextElement(); 
    79     } 
    8091 
    81     length = entry.getSize(); 
    8292    resetStream(); 
     93    populateLength(); 
    8394  } 
    8495 
     
    91102  public ZipHandle(String file, ZipEntry entry) throws IOException { 
    92103    super(); 
    93     zip = new ZipFile(file); 
     104    this.file = file; 
     105    in = new RandomAccessInputStream(getHandle(file)); 
     106    zip = new ZipInputStream(in); 
     107    while (!entry.getName().equals(zip.getNextEntry().getName())); 
     108    entryCount = 1; 
    94109    this.entry = entry; 
    95     length = entry.getSize(); 
    96110    resetStream(); 
     111    populateLength(); 
    97112  } 
    98113 
     
    103118    if (!file.toLowerCase().endsWith(".zip")) return false; 
    104119 
    105     FileInputStream f = new FileInputStream(file); 
     120    IRandomAccess handle = getHandle(file); 
    106121    byte[] b = new byte[2]; 
    107     f.read(b); 
    108     f.close(); 
     122    handle.read(b); 
     123    handle.close(); 
    109124    return new String(b).equals("PK"); 
    110125  } 
     
    122137  /** Returns the number of entries. */ 
    123138  public int getEntryCount() { 
    124     return zip.size(); 
     139    return entryCount; 
    125140  } 
    126141 
     
    133148      zip = null; 
    134149      entry = null; 
     150      if (in != null) in.close(); 
     151      in = null; 
     152      entryCount = 0; 
    135153    } 
    136154  } 
     
    141159  protected void resetStream() throws IOException { 
    142160    if (stream != null) stream.close(); 
     161    if (in != null) { 
     162      in.close(); 
     163      in = new RandomAccessInputStream(getHandle(file)); 
     164    } 
     165    if (zip != null) zip.close(); 
     166    zip = new ZipInputStream(in); 
     167    if (entry != null) { 
     168      while (!entry.getName().equals(zip.getNextEntry().getName())); 
     169    } 
    143170    stream = new DataInputStream(new BufferedInputStream( 
    144       zip.getInputStream(entry), RandomAccessInputStream.MAX_OVERHEAD)); 
     171      zip, RandomAccessInputStream.MAX_OVERHEAD * 10)); 
     172    stream.mark(RandomAccessInputStream.MAX_OVERHEAD * 10); 
     173  } 
     174 
     175  // -- Helper methods -- 
     176 
     177  private void populateLength() throws IOException { 
     178    length = -1; 
     179    while (stream.available() > 0) { 
     180      stream.skip(1); 
     181      length++; 
     182    } 
     183    resetStream(); 
     184  } 
     185 
     186  private static IRandomAccess getHandle(String file) throws IOException { 
     187    return file.startsWith("http://") ? 
     188      new URLHandle(file) : new NIOFileHandle(file, "r"); 
    145189  } 
    146190 
Note: See TracChangeset for help on using the changeset viewer.