gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:50:36 24 4
gpt4 key购买 nike

实际上,我要将 .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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com