java.io.IOException: Zip bomb detected! The file would exceed the max. ratio of compressed file size

在使用Apache POI处理Excel文件时遇到了'java.io.IOException: Zip bomb detected!'错误,该异常指出文件压缩比例超过了最大限制。通过设置ZipSecureFile.setMinInflateRatio(-1.0d)虽然能避免报错,但会导致读取的Excel值为null。解决方法是复制不受保护的Excel文件进行读取。
摘要由CSDN通过智能技术生成

java.io.IOException: Zip bomb detected! The file would exceed the max. ratio of compressed file size to the size of the expanded data.
1、使用poi导入excel文件时碰到如下问题
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: 1935318, Raw/compressed size: 19345, ratio: 0.009996
Limits: MIN_INFLATE_RATIO: 0.010000, Entry: xl/pivotCache/pivotCacheRecords1.xml
	at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.checkThreshold(ZipArchiveThresholdInputStream.java:130)
	at org
要完美处理这个问题,您需要在处理压缩文件之前对文件进行一些检查和限制,以确保文件不会超出系统的最大文件大小限制。 以下是一个可能的解决方案: ```java try (ZipInputStream zis = new ZipInputStream(new FileInputStream("your_zip_file.zip"))) { ZipEntry entry = null; long totalSize = 0; while ((entry = zis.getNextEntry()) != null) { // 检查压缩文件是否超出最大限制 if (entry.getCompressedSize() > MAX_COMPRESSED_SIZE) { throw new IOException("Zip bomb detected! The file would exceed the max. ratio of compressed file size."); } // 检查解压缩后的文件是否超出最大限制 if (entry.getSize() > MAX_UNCOMPRESSED_SIZE) { throw new IOException("Zip bomb detected! The uncompressed file would exceed the max. ratio of file size."); } // 累加压缩文件的大小 totalSize += entry.getCompressedSize(); // 检查总大小是否超出最大限制 if (totalSize > MAX_TOTAL_SIZE) { throw new IOException("Zip bomb detected! The total size of compressed files would exceed the max. ratio of file size."); } // 处理压缩文件 // ... } } catch (IOException e) { // 处理异常 // ... } ``` 在此示例代码中,我们使用ZipInputStream来读取压缩文件,并在每个文件条目上执行以下检查: 1. 检查压缩文件是否超出最大限制。 2. 检查解压缩后的文件是否超出最大限制。 3. 累加压缩文件的大小,并检查总大小是否超出最大限制。 如果任何一个检查失败,就会抛出一个IOException异常。 您需要根据您的需求和环境来调整这些限制。闲话少说,希望这个解决方案能帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值