0

我希望 Tika 只输出存档中文件的名称(如果输入文件是存档),如果输入文件不是存档,则像往常一样输出文件内容。我怎样才能做到这一点?

4

1 回答 1

4

我扩展了 ParsingEmbeddedDocumentExtractor 类

class CustomParsingEmbeddedDocumentExtractor extends ParsingEmbeddedDocumentExtractor {
  public CustomParsingEmbeddedDocumentExtractor(ParseContext context) {
    super(context);
  }

  public void parseEmbedded(
        InputStream stream, ContentHandler handler, Metadata metadata, boolean outputHtml)
        throws SAXException, IOException {


    String name = metadata.get(Metadata.RESOURCE_NAME_KEY);
    if (name != null && name.length() > 0 ) {
        handler.startElement(XHTML, "h1", "h1", new AttributesImpl());
        char[] chars = name.toCharArray();
        handler.characters(chars, 0, chars.length);
        handler.endElement(XHTML, "h1", "h1");
    }

    //Removed the parsing logic here.. We just want the file names..

  }
}

并在执行之前将其设置为 ParseContext 变量someparser.parse()

context.set(EmbeddedDocumentExtractor.class, new CustomParsingEmbeddedDocumentExtractor(this.context));

这仅适用于 zip、tar 和 jar。这对我足够了。

于 2012-10-08T11:36:48.780 回答