简体   繁体   中英

How to search for docx file and unzip it in the given directory using java?

Actually I'm going to convert the .docx file into .xhtml. so I need to find out the doc file in the given directory and unzip it. After getting document.xml from the word file I need that file to process with some xsl files to get final result as .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. For search files by extensions you can use Apache Commons IO library:

method:

FileUtils.iterateFiles(File directory, String[] extensions, boolean recursive)
  1. For zip\\unzip: Apache Commons Compress

ZipArchiveInputStream class

something like that:

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();
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM