@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 testEncryptedRar() throws Exception {
        Parser parser = new RarParser();

        try (InputStream input = 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";
                }
            });

            // Note - we don't currently support encrypted RAR
            // files so we can't check the contents
            parser.parse(input, handler, metadata, trackingContext);
            fail("No support yet for Encrypted RAR files");
        } catch (EncryptedDocumentException e) {
            // Good, as expected right now
        }
    }