Search in sources :

Example 1 with ZipHandle

use of loci.common.ZipHandle in project bioformats by openmicroscopy.

the class ZipReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    reader = new ImageReader();
    reader.setMetadataOptions(getMetadataOptions());
    reader.setMetadataFiltered(isMetadataFiltered());
    reader.setOriginalMetadataPopulated(isOriginalMetadataPopulated());
    reader.setNormalized(isNormalized());
    reader.setMetadataStore(getMetadataStore());
    String innerFile = id;
    if (checkSuffix(id, "zip")) {
        innerFile = id.substring(0, id.length() - 4);
    }
    int sep = innerFile.lastIndexOf(File.separator);
    if (sep < 0) {
        sep = innerFile.lastIndexOf("/");
    }
    if (sep >= 0) {
        innerFile = innerFile.substring(sep + 1);
    }
    // NB: We need a raw handle on the ZIP data itself, not a ZipHandle.
    IRandomAccess rawHandle = Location.getHandle(id, false, false);
    in = new RandomAccessInputStream(rawHandle, id);
    ZipInputStream zip = new ZipInputStream(in);
    ZipEntry ze = null;
    entryName = null;
    boolean matchFound = false;
    while (true) {
        ze = zip.getNextEntry();
        if (ze == null)
            break;
        if (entryName == null) {
            entryName = ze.getName();
        }
        if (!matchFound && ze.getName().startsWith(innerFile)) {
            entryName = ze.getName();
            matchFound = true;
        }
        ZipHandle handle = new ZipHandle(id, ze);
        Location.mapFile(ze.getName(), handle);
        mappedFiles.add(ze.getName());
    }
    if (entryName == null) {
        throw new FormatException("Zip file does not contain any valid files");
    }
    reader.setId(entryName);
    metadataStore = reader.getMetadataStore();
    core = new ArrayList<CoreMetadata>(reader.getCoreMetadataList());
    metadata = reader.getGlobalMetadata();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipHandle(loci.common.ZipHandle) ZipEntry(java.util.zip.ZipEntry) IRandomAccess(loci.common.IRandomAccess) RandomAccessInputStream(loci.common.RandomAccessInputStream) ImageReader(loci.formats.ImageReader) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 IRandomAccess (loci.common.IRandomAccess)1 RandomAccessInputStream (loci.common.RandomAccessInputStream)1 ZipHandle (loci.common.ZipHandle)1 CoreMetadata (loci.formats.CoreMetadata)1 FormatException (loci.formats.FormatException)1 ImageReader (loci.formats.ImageReader)1