Java 读取 Excel 报错

文章讨论了在处理Excel文件时遇到的Zipbombdetected错误,该错误由于压缩文件大小与解压缩后大小的比例超过安全限制导致。ApachePOI的ZipSecureFile类设置了默认阈值,可以通过设置`setMinInflateRatio`方法调整此限制以允许读取异常文件,例如将比例设置为0.01以下。
摘要由CSDN通过智能技术生成

错误信息

Caused by: java.io.IOException: Zip bomb detected! The file would exceed the max. ratio of compressed file size to the size of the expanded data.
This may indicate that the file is used to inflate memory usage and thus could pose a security risk.
You can adjust this limit via ZipSecureFile.setMinInflateRatio() if you need to work with files which exceed this limit.
Uncompressed size: 133724, Raw/compressed size: 1328, ratio: 0.009931
Limits: MIN_INFLATE_RATIO: 0.010000, Entry: xl/styles.xml

Caused by: java.io.IOException: Zip bomb detected! The file would exceed the max. ratio of compressed file size to the size of the expanded data.
This may indicate that the file is used to inflate memory usage and thus could pose a security risk.
You can adjust this limit via ZipSecureFile.setMinInflateRatio() if you need to work with files which exceed this limit.
Uncompressed size: 133724, Raw/compressed size: 1328, ratio: 0.009931
Limits: MIN_INFLATE_RATIO: 0.010000, Entry: xl/styles.xml

分析

查看 org.apache.poi.openxml4j.util 包下 ZipSecureFile 类,可以看到此属性默认值为 static double MIN_INFLATE_RATIO = 0.01(和错误提示一致),在这个类中可以找到有 setMinInflateRatio 方法可以对此属性值进行修改,源码如下:

public class ZipSecureFile extends ZipFile {
    static double MIN_INFLATE_RATIO = 0.01;
    static long MAX_ENTRY_SIZE = 4294967295L;
	...
    public static void setMinInflateRatio(double ratio) {
        MIN_INFLATE_RATIO = ratio;
    }
    ...
}

解决方案

在读取 Excel 前通过 ZipSecureFile.setMinInflateRatio(ratio) 即可解决(将 ratio 设为报错信息提示的阈值以下就行,如这里提示的阈值是 MIN_INFLATE_RATIO: 0.010000),比如将 ratio 设为 0

ZipSecureFile.setMinInflateRatio(0);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值