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

时间:2016-03-16 05:18:19

标签: java xml xslt

实际上我要将.docx文件转换为.xhtml。所以我需要找出给定目录中的doc文件并解压缩。从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 个答案:

答案 0 :(得分:0)

  1. 对于按扩展名搜索的文件,您可以使用Apache Commons IO库:
  2. 方法:

    FileUtils.iterateFiles(File directory, String[] extensions, boolean recursive)
    
    1. 对于zip \ unzip:Apache Commons Compress
    2. 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();
                      }
                  }
              }
          }
      }