Example #1
0
  /* @see loci.formats.FormatReader#initFile(String) */
  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());

    // 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);
    while (true) {
      ZipEntry ze = zip.getNextEntry();
      if (ze == null) break;
      ZipHandle handle = new ZipHandle(id, ze);
      Location.mapFile(ze.getName(), handle);
      mappedFiles.add(ze.getName());
    }

    ZipHandle base = new ZipHandle(id);
    reader.setId(base.getEntryName());

    metadataStore = reader.getMetadataStore();
    core = reader.getCoreMetadata();
    metadata = reader.getGlobalMetadata();

    base.close();
  }
  /**
   * Gets an IRandomAccess object that can read from or write to the given file.
   *
   * @see IRandomAccess
   */
  public static IRandomAccess getHandle(String id, boolean writable, boolean allowArchiveHandles)
      throws IOException {
    LOGGER.trace("getHandle(id = {}, writable = {})", id, writable);
    IRandomAccess handle = getMappedFile(id);
    if (handle == null) {
      LOGGER.trace("no handle was mapped for this ID");
      String mapId = getMappedId(id);

      if (id.startsWith("http://")) {
        handle = new URLHandle(mapId);
      } else if (allowArchiveHandles && ZipHandle.isZipFile(id)) {
        handle = new ZipHandle(mapId);
      } else if (allowArchiveHandles && GZipHandle.isGZipFile(id)) {
        handle = new GZipHandle(mapId);
      } else if (allowArchiveHandles && BZip2Handle.isBZip2File(id)) {
        handle = new BZip2Handle(mapId);
      } else {
        handle = new NIOFileHandle(mapId, writable ? "rw" : "r");
      }
    }
    LOGGER.trace("Location.getHandle: {} -> {}", id, handle);
    return handle;
  }
 @Test
 public void testZipTypeDetection() throws IOException {
   File invalidFile = File.createTempFile("invalid", ".zip");
   invalidFile.deleteOnExit();
   assertEquals(ZipHandle.isZipFile(invalidFile.getAbsolutePath()), false);
 }