a396100265
年轻的小老弟
2017-10-25 09:35

POI读取通过xml修改的docx文件报错。

  • xml
  • docx4j
  • poi
  • word
  • zip4j

我通过zip4j解压docx,修改其中的document.xml文件来实现编辑word。然后我将修改完的文件压缩,然后通过POI来读取,报错:
[code=java]Exception in thread "main" java.io.IOException: Failed to read zip entry source
at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:103)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xwpf.usermodel.XWPFDocument.(XWPFDocument.java:116)
at test.Test1.test(Test1.java:36)
at test.Test1.main(Test1.java:29)
Caused by: java.util.zip.ZipException: only DEFLATED entries can have EXT descriptor
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:310)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:122)
at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.getNextEntry(ZipSecureFile.java:280)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.(ZipInputStreamZipEntrySource.java:52)
at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:100)
... 5 more[/code]
zip4j压缩代码:
[code=java] ZipFile zipFile = new ZipFile(zipPath);

    ZipParameters parameters = new ZipParameters();
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    parameters.setIncludeRootFolder(false);

    zipFile.addFolder(dirPath, parameters);[/code]

POI读取代码:
[code=java] XWPFWordExtractor extractor = new XWPFWordExtractor(new XWPFDocument(new FileInputStream(new File(f))));
System.out.println(extractor.getText());[/code]
错误信息是在看不懂,zip4j的压缩方式和级别为几乎都试过的,还是不行。但是我用WPS打开zip4j压缩的docx是没有问题的。就是通过POI才会报错。
后来找到docx4j这个工具,但是资料比较少,不知道怎么用,大佬有资料分享吗?
有大佬知道的原因吗?或者有更好的方法来实现将word中的图片替换为文字。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答