@Test
  public void identifyGZipArchiveTest() throws CommandExecutionException {

    String fileName;
    File file = new File(tarFile);
    if (!file.exists()) {
      fail("TAR test file not found");
    }
    URI uri = file.toURI();
    RequestIdentifier identifier = new RequestIdentifier(uri);
    identifier.setParentId(1L);
    try {
      fileName = file.getCanonicalPath();
      RequestMetaData metaData = new RequestMetaData(file.length(), file.lastModified(), fileName);
      FileSystemIdentificationRequest request =
          new FileSystemIdentificationRequest(metaData, identifier);

      InputStream tarStream = new FileInputStream(file);
      request.open(tarStream);
      tarArchiveContentIdentifier.identify(uri, request);

    } catch (IOException e) {
      throw new CommandExecutionException(e);
    }
  }
Esempio n. 2
0
  /** Query a file and get back an XML response. */
  public IdentificationResultCollection queryFile(File fil) throws IOException {
    RequestMetaData metadata = new RequestMetaData(fil.length(), fil.lastModified(), fil.getName());
    RequestIdentifier identifier = new RequestIdentifier(fil.toURI());
    FileInputStream in = null;
    FileSystemIdentificationRequest req = null;
    try {
      req = new FileSystemIdentificationRequest(metadata, identifier);
      in = new FileInputStream(fil);
      req.open(in);
      IdentificationResultCollection results;
      results = sigIdentifier.matchBinarySignatures(req);
      if (results.getResults().size() > 1) {
        sigIdentifier.removeLowerPriorityHits(results);
      }

      if (results.getResults().size() == 0) {
        results = sigIdentifier.matchExtensions(req, false);
      }
      if (results.getResults().size() > 1) {
        sigIdentifier.removeLowerPriorityHits(results);
      }

      //        List<IdentificationResult> resultsList = results.getResults();
      // This gives us an unfiltered list of matching signatures
      return results;
    } finally {
      if (req != null) {
        req.close();
      }
      if (in != null) {
        in.close();
      }
    }
  }
  @Test
  public void identifyZipFileTest() throws CommandExecutionException {

    zipIdentifierEngine.setRequestFactory(requestFactory);
    zipContainerContentIdentifier.setIdentifierEngine(zipIdentifierEngine);
    Object object = zipContainerContentIdentifier.getIdentifierEngine();
    assertEquals(object.getClass(), zipIdentifierEngine.getClass());

    try {
      InputStream in = new FileInputStream(containerSignatures);
      ContainerSignatureSaxParser parser = new ContainerSignatureSaxParser();
      containerSignatureDefinitions = parser.parse(in);
    } catch (SignatureParseException e) {
      throw new CommandExecutionException("Can't parse container signature file");
    } catch (Exception e) {
      throw new CommandExecutionException(e);
    }

    zipContainerContentIdentifier.init(containerSignatureDefinitions, "ZIP");
    object = zipContainerContentIdentifier.getContainerIdentifierInit();
    assertEquals(object.getClass(), containerIdentifierInit.getClass());

    object = zipContainerContentIdentifier.getFormats();
    assertEquals(object.getClass(), formats.getClass());

    String fileName;
    File file = new File(zipFile);
    if (!file.exists()) {
      fail("ZIP test file not found");
    }
    URI uri = file.toURI();
    RequestIdentifier identifier = new RequestIdentifier(uri);
    identifier.setParentId(1L);
    try {
      fileName = file.getCanonicalPath();
      RequestMetaData metaData = new RequestMetaData(file.length(), file.lastModified(), fileName);
      FileSystemIdentificationRequest request =
          new FileSystemIdentificationRequest(metaData, identifier);
      IdentificationResultCollection results = new IdentificationResultCollection(request);
      request.open(file);
      results = zipContainerContentIdentifier.process(request.getSourceInputStream(), results);
      if (results.getResults().isEmpty()) {
        fail("ZIP file not identified");
      }
      String fmtExpected = "";
      for (IdentificationResult identResult : results.getResults()) {
        if (identResult.getPuid().equals("fmt/189")) {
          fmtExpected = identResult.getPuid();
        }
      }
      assertEquals(fmtExpected, "fmt/189");
    } catch (IOException e) {
      throw new CommandExecutionException(e);
    }
  }