错误信息
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);