EasyExcel 遭遇 java.io.IOException: Zip bomb detected

  1. 报错
    Caused by: com.alibaba.excel.exception.ExcelAnalysisException: 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.
    Counter: 819230, cis.counter: 8192, ratio: 0.009999633802473054
    Limits: MIN_INFLATE_RATIO: 0.01
  2. 解决
    在报错代码上一行加上,问题得以解决(阅读报错信息,发现 ratio: 0.009999633802473054,而Limits: MIN_INFLATE_RATIO: 0.01,setMinInflateRatio() if you need to work with files which exceed this limit)
ZipSecureFile.setMinInflateRatio(0.009);
  1. 在网上搜索其他两种设置: 代表将接受所有可能的比率
    3.1. ZipSecureFile.setMinInflateRatio(-1.0d)
    3.2. ZipSecureFile.setMinInflateRatio(0)
使用EasyExcel读取Excel模板并将其内容映射到Map中涉及以下几个步骤[^1]: 1. **添加依赖**: 如果还没有集成EasyExcel,需要先在项目中添加Spring Cloud Alibaba的Alibaba-EasyExcel依赖。 2. **配置读取器**: 创建一个`DataMapper`接口,用于定义从Excel单元格到Java Bean属性的映射关系: ```java public interface DataMapper { Map<String, Object> mapToEntity(Record record); } ``` 3. **创建实体类**: 基于模板创建一个Java Bean,例如`MyEntity`,每个字段对应Excel表的一列。 4. **设置读取策略**: 在`@ExcelReader`注解中指定模板名称、数据映射方式以及是否忽略空值: ```java @ExcelReader("template.xlsx", ignoreEmptyValue = true) public List<MyEntity> readFromTemplate(@Autowired DataMapper dataMapper) throws IOException { // 实际读取操作 } ``` 5. **解析模板到Map**: 在`readFromTemplate`方法内部,使用`dataMapper.mapToEntity(record)`将每一行`Record`转换为`Map`: ```java List<Record> records = ...; // 通过EasyExcel读取得到的数据记录列表 List<Map<String, Object>> maps = new ArrayList<>(); for (Record record : records) { maps.add(dataMapper.mapToEntity(record)); } ``` 6. **处理Map数据**: 获取到`Map`后,你可以根据实际需求进一步处理,如存储到数据库或其他地方。 如果读取时遇到"Zip bomb detected!"错误[^2],可能是由于Excel文件大小超过了允许的压缩比限制,可以尝试减小文件大小,优化模板设计,或者调整EasyExcel的配置以适应大文件处理。具体解决方法可能包括分割大文件、调整压缩算法设置等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值