@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, metadata);
            rar = new Archive(tis.getFile());

            if (rar.isEncrypted()) {
                throw new EncryptedDocumentException();
            }

            //Without this BodyContentHandler does not work
            xhtml.element("div", " ");

            FileHeader header = rar.nextFileHeader();
            while (header != null && !Thread.currentThread().isInterrupted()) {
                if (!header.isDirectory()) {
                    Metadata entrydata = PackageParser.handleEntryMetadata(header.getFileName(),
                            header.getCTime(), header.getMTime(), header.getFullUnpackSize(),
                            xhtml);
                    try (InputStream subFile = rar.getInputStream(header)) {
                        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();
    }
@Test
    public void testEmbedded() throws Exception {
        ContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();

        try (InputStream stream = getResourceAsStream("/test-documents/test-documents.rar")) {
            AUTO_DETECT_PARSER.parse(stream, handler, metadata, trackingContext);
        }

        // Should have found all 9 documents, but not the directory
        assertEquals(9, tracker.filenames.size());
        assertEquals(9, tracker.mediatypes.size());
        assertEquals(9, tracker.modifiedAts.size());

        // Should have names but not content types, as rar doesn't
        //  store the content types
        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(mod.startsWith("20"), "Modified at " + mod);
        }

        // Should have filenames in the content string
        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);
    }