Skip to content

‘ZipException: invalid code lengths set’ when streaming input file to Apache-POI

I’m attempting to create an xlsx workbook object with Apache-POI by passing my .xlsx as a resource stream:

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

I can successfully instantiate the workbook when passing my template.xlsx through a FileInputStream and a local file path, but when I pass the resources stream I get an exception:

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

The .xlsx I’m passing shouldn’t be zipped, but maybe that’s how it works with the resource stream. Any ideas on how to pass a file into Apache-POI via resource access?

Answer

Turns out you have to unfilter binary files when using maven resources.

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