java - 如何使用 java 搜索 docx 文件并将其解压缩到给定目录中?

标签 java xml xslt

实际上,我要将 .docx 文件转换为 .xhtml。所以我需要找到给定目录中的文档文件并解压缩。从 word 文件中获取 document.xml 后,我需要该文件处理一些 xsl 文件以获得 .xhtml 的最终结果。

//-------------------------Source--------------------------------------               
simpleTransform(Source+"/"+"filename"+".xml", Source+"/01-W2H.xslt", Source+"/"+"out2.xml");
simpleTransform(Source+"/"+"out2.xml", Source+"/08-xmlns.xslt", Source+"/"+"out22.xml");
simpleTransform(Source+"/"+"out22.xml", Source+"/06-Heading.xslt", Source+"/"+"out3.xml");
simpleTransform(Source+"/"+"out222.xml", Source+"/02-FigureRef.xsl", Source+"/"+"out222.xml");
simpleTransform(Source+"/"+"out3", Source+"/03-Remove-Duplecate.xsl", Source+"/"+"OUT4.xml");
simpleTransform(Source+"/"+"out4.xml", Source+"/04-Return_XML_To_Position.xsl", Source+"/"+"OUT5.xml");
simpleTransform(Source+"/"+"out5.xml", Source+"/05-Final.xsl", Source+"/"+"filename"+".xhtml");       
simpleTransform(Source+"/com.apple.ibooks.display-options.xm", Source+"/07-Combine.xsl", Source+"/"+"del.txt");
simpleTransform(Source+"/"+"merged-html.xml", Source+"/08-xmlns.xslt", Source+"/"+"merged-html2.xml");
simpleTransform(Source+"/"+"merged-html2.xml", Source+"/09-OPF.xsl", Source+"/"+"del2.txt");
simpleTransform(Source+"/merged-html2.xml", Source+"/10-NCX.xsl", Source+"/del3.xml");
simpleTransform(Source+"/toc.ncx", Source+"/10-NCX2.xsl", Source+"/toc.ncx");
simpleTransform(Source+"/toc.ncx", Source+"/11-Heading.xslt", Source+"/toc.ncx");
simpleTransform(Source+"/toc.ncx", Source+"/11-idsequence.xsl",  Source+"/toc.ncx");          
simpleTransform(Source+"/merged-html2.xml", Source+"/12-Contents.xsl",  Source+"/contents.xhtml");

最佳答案

  1. 要按扩展名搜索文件,您可以使用 Apache Commons IO图书馆:

方法:

FileUtils.iterateFiles(File directory, String[] extensions, boolean recursive)
  1. 对于压缩\解压缩:Apache Commons Compress

ZipArchiveInputStream

类似的东西:

try(ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
    ZipArchiveEntry zipEntry;

    while ((zipEntry = zipArchiveInputStream.getNextZipEntry()) != null){
        String fileName = zipEntry.getName();

        final File file = new File(fileName);

        FileUtil.createMissingParentDirectories(file);

        try(FileOutputStream fileOutputStream = new FileOutputStream(file.getAbsolutePath())) {(file.read.buffer)
            try(BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream, 1024)) {
                int n = 0;

                byte[] content = new byte[1024];

                while ((n = zipArchiveInputStream.read(content)) != -1) {
                    fileOutputStream.write(content, 0, n);
                }

                bos.flush();
                }
            }
        }
    }
}

关于java - 如何使用 java 搜索 docx 文件并将其解压缩到给定目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36027339/

相关文章:

java - 如何在没有entryId的情况下更新SQLiteOpenHelper中的条目?

java - 如何在没有 Http 请求的情况下获取 Web 应用程序的当前上下文?

xslt - 使用 XSLT 处理循环依赖

java - 禁用 ez-vcard PROPID

java - 在android中替换TextWatcher中的字符

java - 使用 MOXy 和 XPath,是否可以解码属性列表?

xml - HTML/HTML5 的默认命名空间是什么?

java - 如何使用 XMLStreamWriter 编写自闭合元素标签

google-chrome - 为什么我不能让XSLT在chrome中工作?

xml - 使用 XSLT 展平 XML,但基于嵌套级别