@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);

        Path cwd = Files.createTempDirectory("tika-unrar-");
        try {
            Path tmp = Files.createTempFile(cwd, "input", ".rar");
            try (OutputStream os = Files.newOutputStream(tmp, StandardOpenOption.WRITE)) {
                IOUtils.copy(stream, os);
            }
            FileProcessResult result = unrar(cwd, tmp);
            //delete the tmp rar file so that we don't recursively parse it in the next step
            try {
                Files.delete(tmp);
            } catch (IOException e) {
                //warn failed to delete tmp
            }
            if (result.isTimeout()) {
                throw new TikaTimeoutException("timed out unrarring");
            } else if (result.getExitValue() != 0) {
                if (result.getStderr().contains("error in the encrypted file")) {
                    throw new EncryptedDocumentException();
                }
                String msg = result.getStderr();
                if (msg.length() > 100) {
                    msg = msg.substring(0, 100);
                }
                throw new TikaException("Unrecoverable problem with rar file, exitValue=" +
                        result.getExitValue() + " : " + msg);
            }
            //TODO: process stdout to extract status for each file:
            //e.g. Extracting  test-documents/testEXCEL.xls                              OK
            processDirectory(cwd, cwd, xhtml, extractor, context);
        } finally {
            FileUtils.deleteDirectory(cwd.toFile());
        }
        xhtml.endDocument();
    }
@Test
    public void testEncryptedRar() throws Exception {
        assumeTrue(ExternalParser.check("unrar"));
        Parser parser = new UnrarParser();

        try (InputStream input = getResourceAsStream("/test-documents/test-documents-enc.rar")) {
            Metadata metadata = new Metadata();
            ContentHandler handler = new BodyContentHandler();

            // 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
        }
    }