java - 'ZipException : invalid code lengths set' when streaming input file to Apache-POI

标签 java apache stream apache-poi xlsx

我尝试通过将 .xlsx 作为资源流传递来使用 Apache-POI 创建 xlsx 工作簿对象:

Workbook workbook = WorkbookFactory.create(RFQGenerator.class.getResourceAsStream("/com/lumos/xlsx_manager/es/da/template.xlsx"));

通过 FileInputStream 和本地文件路径传递 template.xlsx 时,我可以成功实例化工作簿,但是当我传递资源流时,出现异常:

java.io.IOException: Failed to read zip entry source
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:103)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:184)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:149)
at com.lumos.xlsx_manager.es.da.RFQGenerator.createRfq(RFQGenerator.java:30)
at com.lumos.xlsx_manager.es.da.Firebase$2.onChildAdded(Firebase.java:88)
at com.google.firebase.database.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:81)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:65)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.zip.ZipException: invalid code lengths set
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:194)
at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:220)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:132)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:56)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:100)
... 11 more

我传递的 .xlsx 不应被压缩,但也许这就是它与资源流一起工作的方式。关于如何通过资源访问将文件传递到 Apache-POI 有什么想法吗?

最佳答案

事实证明,使用 Maven 资源时必须取消过滤二进制文件。

Maven corrupting binary files in source/main/resources when building jar

关于java - 'ZipException : invalid code lengths set' when streaming input file to Apache-POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47011240/

相关文章:

apache - RewriteRule 不排除现有文件/目录

在 apache 上使用 UI 路由器的 AngularJS HTML5 模式

python - 如何将文本文件读入文本框?

java - 在两个 Play Framework 应用程序之间共享模型的最佳方式是什么?

java - 关于 Grails 服务类 Singleton 属性

java - 在 Java 中获取哈希表中的键的索引

Perl CGI 通过网络浏览器下载文件

c# - 尝试使用 Newtonsoft 将对象序列化为流,得到一个空流

c++ - 在 GCC 4.7.2 和 Clang 3.2 中显然缺少 getline() 的重载,将 RRef 流式传输

java - Spring Data JPA 使用 EclipseLink 作为 JPA 提供者 : possible bug in EclipseLink?