java.util.zip.ZipException: invalid code lengths set with XSSFWorkbook

Viewed 1589

I am trying to read an .xlsx file from my resources folder but when I try to create the XSSFWorkbook it gives me the following error:

java.io.IOException: Failed to read zip entry source
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:106)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:342)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:285)
    ...
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)
    ...

The code I currently have simply does this:

public Object prueba() {
    
    InputStream is = this.getClass().getResourceAsStream("/excels/test.xlsx");
    XSSFWorkbook workbook = null;
    String response = null;
    try {
        workbook = new XSSFWorkbook(is); <-- Here the error occurs
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFRow row = sheet.getRow(0);
        response = row.getCell(9).getRawValue();
        
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;
}

And in debug mode the variable is if it has content

enter image description here

I also added this to my pom.xml

<resource>
  <directory>${project.basedir}/src/main/resources</directory>
  <filtering>false</filtering>
  <includes>
    <include>**/*.xlsx</include>
  </includes>
</resource>
1 Answers

If this is happening, it may be that the maven-resources-plugin is performing "filtering" on your (binary) spreadsheet files, and thus corrupting them. You can fix it by configuring maven-resources-plugin not to filter Excel files (ie copy them without filtering):

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>**/*.xls</exclude>
                    <exclude>**/*.xlsx</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.xls</include>
                    <include>**/*.xlsx</include>
                </includes>
            </resource>
        </resources>
    </build>

It's probably better to configure maven-resources-plugin only to filter the text-based file types you need (eg Properties, XML, YAML). Note that I haven't tested this config:

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>**/*.properties</exclude>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.yml</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
Related