POI之java.io.IOException: ZIP entry size is too large

问题提出

在基于Java 的POI进行excel的读写之时,碰到的如下问题:

java.io.IOException: Failed to read zip entry source
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:106) ~[poi-ooxml-3.15.jar:3.15]
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:342) ~[poi-ooxml-3.15.jar:3.15]
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) ~[poi-ooxml-3.15.jar:3.15]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:285) ~[poi-ooxml-3.15.jar:3.15]
    at com.fish.cdc.data.std.controller.DemoController.importData(DemoController.java:51) ~[classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
.....................
Caused by: java.io.IOException: ZIP entry size is too large
    at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:122) ~[poi-ooxml-3.15.jar:3.15]
    at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:56) ~[poi-ooxml-3.15.jar:3.15]
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:99) ~[poi-ooxml-3.15.jar:3.15]
    ... 93 more

项目分析

项目是基于Maven来管理的Java项目,代码读写的逻辑非常简单,主要如下:

FileInputStream file = new FileInputStream(ResourceUtils.getFile("classpath:data.xlsx"));
        Workbook workbook = new XSSFWorkbook(file);
        Sheet sheet = workbook.getSheetAt(0);

        List<StandardBean> dataBeans = new ArrayList<StandardBean>();
        for (Row row : sheet) {
            StandardBean stdBean = new StandardBean();
            stdBean.setSeqNo(row.getCell(0).getStringCellValue());
            ............
        }

问题分析

难道是文件损坏了吗?从字面意思来理解,的确是如此的。那为什么会被损坏呢? 考虑到maven项目中打开了filtering的特性,就是会针对项目的资源文件进行扫描,一般的文件,eot文件等等都会受到影响,从而导致了文件无法被正确的解析。

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                ..............
                </resource>
</resources>

pom.xml文件中的resource内容。

问题的解决

既然知道了原因,则就可以在filtering中新增exclude/include规则即可:

<resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/*.xlsx</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>**/*.xlsx</include>
            </includes>
        </resource>
</resources>

总结

由于maven的filtering特性,造成文件损坏的例子很多,一般都是要进行exclude/include操作。

Reference

  1. https://stackoverflow.com/questions/25711507/poi-zip-entry-size-is-too-large

猜你喜欢

转载自blog.csdn.net/blueheart20/article/details/80855631