RarParser extends AbstractParser { @Override public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); EmbeddedDocumentExtractor extractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context); String mediaType = metadata.get(Metadata.CONTENT_TYPE); if (mediaType != null && mediaType.contains("version=5")) { throw new UnsupportedFormatException("Tika does not yet support rar version 5."); } Archive rar = null; try (TemporaryResources tmp = new TemporaryResources()) { TikaInputStream tis = TikaInputStream.get(stream, tmp); rar = new Archive(tis.getFile()); if (rar.isEncrypted()) { throw new EncryptedDocumentException(); } xhtml.element("div", " "); FileHeader header = rar.nextFileHeader(); while (header != null && !Thread.currentThread().isInterrupted()) { if (!header.isDirectory()) { try (InputStream subFile = rar.getInputStream(header)) { Metadata entrydata = PackageParser.handleEntryMetadata( "".equals(header.getFileNameW()) ? header.getFileNameString() : header.getFileNameW(), header.getCTime(), header.getMTime(), header.getFullUnpackSize(), xhtml ); if (extractor.shouldParseEmbedded(entrydata)) { extractor.parseEmbedded(subFile, handler, entrydata, true); } } } header = rar.nextFileHeader(); } } catch (RarException e) { throw new TikaException("RarParser Exception", e); } finally { if (rar != null) rar.close(); } xhtml.endDocument(); }  @Override Set<MediaType> getSupportedTypes(ParseContext arg0); @Override void parse(InputStream stream, ContentHandler handler,
            Metadata metadata, ParseContext context);  }
@Test public void testEmbedded() throws Exception { ContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); try (InputStream stream = RarParserTest.class.getResourceAsStream( "/test-documents/test-documents.rar")) { AUTO_DETECT_PARSER.parse(stream, handler, metadata, trackingContext); } assertEquals(9, tracker.filenames.size()); assertEquals(9, tracker.mediatypes.size()); assertEquals(9, tracker.modifiedAts.size()); assertEquals("test-documents/testEXCEL.xls", tracker.filenames.get(0)); assertEquals("test-documents/testHTML.html", tracker.filenames.get(1)); assertEquals("test-documents/testOpenOffice2.odt", tracker.filenames.get(2)); assertEquals("test-documents/testPDF.pdf", tracker.filenames.get(3)); assertEquals("test-documents/testPPT.ppt", tracker.filenames.get(4)); assertEquals("test-documents/testRTF.rtf", tracker.filenames.get(5)); assertEquals("test-documents/testTXT.txt", tracker.filenames.get(6)); assertEquals("test-documents/testWORD.doc", tracker.filenames.get(7)); assertEquals("test-documents/testXML.xml", tracker.filenames.get(8)); for(String type : tracker.mediatypes) { assertNull(type); } for(String crt : tracker.createdAts) { assertNull(crt); } for(String mod : tracker.modifiedAts) { assertNotNull(mod); assertTrue("Modified at " + mod, mod.startsWith("20")); } String content = handler.toString(); assertContains("test-documents/testHTML.html", content); assertContains("test-documents/testEXCEL.xls", content); assertContains("test-documents/testOpenOffice2.odt", content); assertContains("test-documents/testPDF.pdf", content); assertContains("test-documents/testPPT.ppt", content); assertContains("test-documents/testRTF.rtf", content); assertContains("test-documents/testTXT.txt", content); assertContains("test-documents/testWORD.doc", content); assertContains("test-documents/testXML.xml", content); }
@Test public void testEncryptedRar() throws Exception { Parser parser = new RarParser(); try (InputStream input = RarParserTest.class.getResourceAsStream( "/test-documents/test-documents-enc.rar")) { Metadata metadata = new Metadata(); ContentHandler handler = new BodyContentHandler(); ParseContext context = new ParseContext(); context.set(PasswordProvider.class, new PasswordProvider() { @Override public String getPassword(Metadata metadata) { return "ApacheTika"; } }); parser.parse(input, handler, metadata, trackingContext); fail("No support yet for Encrypted RAR files"); } catch (EncryptedDocumentException e) { } }