Changeset 6121


Ignore:
Timestamp:
04/13/10 16:01:25 (10 years ago)
Author:
melissa
Message:

Preliminary support for reading Zip files with multiple entries. Closes #455.

Location:
trunk/components
Files:
5 edited

Legend:

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

    r6078 r6121  
    2525 
    2626import java.io.IOException; 
     27import java.util.Enumeration; 
     28import java.util.zip.ZipEntry; 
     29import java.util.zip.ZipFile; 
    2730 
    2831import loci.common.Location; 
     
    6972  } 
    7073 
    71   /* @see loci.formats.IFormatReader#getSeriesUsedFiles(boolean) */ 
    72   public String[] getSeriesUsedFiles(boolean noPixels) { 
    73     return reader.getSeriesUsedFiles(noPixels); 
    74   } 
    75  
    7674  /** 
    7775   * @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int) 
     
    103101    reader.setMetadataStore(getMetadataStore()); 
    104102 
    105     ZipHandle zip = new ZipHandle(id); 
    106     if (zip.getEntryCount() > 1) { 
    107       throw new FormatException( 
    108         "Zip files that contain more than one file are not supported."); 
     103    ZipFile zip = new ZipFile(id); 
     104    Enumeration<? extends ZipEntry> e = zip.entries(); 
     105    while (e.hasMoreElements()) { 
     106      ZipEntry ze = e.nextElement(); 
     107      ZipHandle handle = new ZipHandle(id, ze); 
     108      Location.mapFile(ze.getName(), handle); 
    109109    } 
    110     String name = zip.getEntryName(); 
    111     Location.mapFile(name, zip); 
    112     reader.setId(name); 
     110 
     111    ZipHandle base = new ZipHandle(id); 
     112    reader.setId(base.getEntryName()); 
    113113 
    114114    metadataStore = reader.getMetadataStore(); 
  • trunk/components/common/src/loci/common/Location.java

    r6085 r6121  
    344344      } 
    345345    } 
    346     return file.exists(); 
     346    if (file.exists()) return true; 
     347    if (getMappedFile(file.getPath()) != null) return true; 
     348 
     349    String mappedId = getMappedId(file.getPath()); 
     350    return mappedId != null && new File(mappedId).exists(); 
    347351  } 
    348352 
  • trunk/components/common/src/loci/common/RandomAccessInputStream.java

    r6053 r6121  
    7777    raf = handle; 
    7878    raf.setOrder(ByteOrder.BIG_ENDIAN); 
     79    seek(0); 
    7980  } 
    8081 
  • trunk/components/common/src/loci/common/StreamHandle.java

    r6050 r6121  
    264264  /* @see java.io.DataInput#readUnsignedShort() */ 
    265265  public int readUnsignedShort() throws IOException { 
    266     fp += 2; 
    267     int v = stream.readUnsignedShort(); 
    268     return order.equals(ByteOrder.LITTLE_ENDIAN) ? DataTools.swap(v) : v; 
     266    return readShort() & 0xffff; 
    269267  } 
    270268 
  • trunk/components/common/src/loci/common/ZipHandle.java

    r6050 r6121  
    5454  // -- Constructor -- 
    5555 
    56   /** 
    57    * Constructs a new ZipHandle corresponding to the given file. 
    58    * 
    59    * @throws HandleException if: 
    60    *   <li>The given file is not a Zip file.<br> 
    61    *   <li>The Zip file contains more than one entry.<br> 
    62    */ 
    6356  public ZipHandle(String file) throws IOException { 
    6457    super(); 
    65     this.file = file; 
    66     if (!isZipFile(file)) { 
    67       throw new HandleException(file + " is not a Zip file."); 
    68     } 
    6958    zip = new ZipFile(file); 
    7059 
     
    8574        break; 
    8675      } 
    87       else if (entry == null) entry = ze; // default to first entry 
    8876    } 
    8977    if (entry == null) { 
    90       throw new HandleException("Zip file '" + file + "' has no entries"); 
     78      entry = zip.entries().nextElement(); 
    9179    } 
    9280 
     81    length = entry.getSize(); 
     82    resetStream(); 
     83  } 
     84 
     85  /** 
     86   * Constructs a new ZipHandle corresponding to the given file. 
     87   * 
     88   * @throws HandleException if: 
     89   *   <li>The given file is not a Zip file.<br> 
     90   */ 
     91  public ZipHandle(String file, ZipEntry entry) throws IOException { 
     92    super(); 
     93    zip = new ZipFile(file); 
     94    this.entry = entry; 
    9395    length = entry.getSize(); 
    9496    resetStream(); 
     
    108110  } 
    109111 
    110   /** Get the name of the first entry. */ 
     112  /** Get the name of the backing Zip entry. */ 
    111113  public String getEntryName() { 
    112114    return entry.getName(); 
    113115  } 
    114116 
    115   /** Returns the DataInputStream corresponding to the first entry. */ 
     117  /** Returns the DataInputStream corresponding to the backing Zip entry. */ 
    116118  public DataInputStream getInputStream() { 
    117119    return stream; 
     
    127129  /* @see IRandomAccess#close() */ 
    128130  public void close() throws IOException { 
    129     super.close(); 
    130     zip = null; 
    131     entry = null; 
     131    if (!Location.getIdMap().containsValue(this)) { 
     132      super.close(); 
     133      zip = null; 
     134      entry = null; 
     135    } 
    132136  } 
    133137 
     
    138142    if (stream != null) stream.close(); 
    139143    stream = new DataInputStream(new BufferedInputStream( 
    140      zip.getInputStream(entry), RandomAccessInputStream.MAX_OVERHEAD)); 
     144      zip.getInputStream(entry), RandomAccessInputStream.MAX_OVERHEAD)); 
    141145  } 
    142146 
Note: See TracChangeset for help on using the changeset viewer.