使用Hutool操作Excel的时候出现的问题(压缩比问题)

server/2024/9/22 15:58:45/

今天在使用Hutool操作Excel的时候,出现了一个问题,导致操作失败。

错误原因如下:

cn.hutool.poi.exceptions.POIException: 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: 158224, Raw/compressed size: 1572, ratio: 0.009935
Limits: MIN_INFLATE_RATIO: 0.010000, Entry: xl/styles.xmlat cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:148)at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:133)at cn.hutool.poi.excel.WorkbookUtil.createBookForWriter(WorkbookUtil.java:92)at cn.hutool.poi.excel.ExcelWriter.<init>(ExcelWriter.java:158)at cn.hutool.poi.excel.ExcelWriter.<init>(ExcelWriter.java:139)at cn.hutool.poi.excel.ExcelWriter.<init>(ExcelWriter.java:116)at cn.hutool.poi.excel.ExcelUtil.getWriter(ExcelUtil.java:289)at com.ppgod.common.kf.OpenDataReport.writeAllData(OpenDataReport.java:370)at com.ppgod.common.kf.OpenDataReport.testIneTableName(OpenDataReport.java:230)at com.ppgod.common.kf.OpenDataReport.writeExcel(OpenDataReport.java:191)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:213)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:138)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)at java.util.ArrayList.forEach(ArrayList.java:1259)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)at java.util.ArrayList.forEach(ArrayList.java:1259)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:147)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:127)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:90)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102)at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
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: 158224, Raw/compressed size: 1572, ratio: 0.009935
Limits: MIN_INFLATE_RATIO: 0.010000, Entry: xl/styles.xmlat org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.checkThreshold(ZipArchiveThresholdInputStream.java:132)at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.read(ZipArchiveThresholdInputStream.java:82)at org.apache.poi.util.IOUtils.toByteArray(IOUtils.java:183)at org.apache.poi.util.IOUtils.toByteArray(IOUtils.java:150)at org.apache.poi.util.IOUtils.toByteArray(IOUtils.java:137)at org.apache.poi.openxml4j.util.ZipArchiveFakeEntry.<init>(ZipArchiveFakeEntry.java:47)at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:53)at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:106)at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:312)at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:97)at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:36)at org.apache.poi.ss.usermodel.WorkbookFactory.lambda$create$2(WorkbookFactory.java:199)at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:295)at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:199)at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:146)... 77 more

错误信息表明,尝试打开的 Excel 文件 (xl/styles.xml) 在压缩前的大小为 158,224 字节,而压缩后的大小仅为 1,572 字节,压缩比约为 0.009935。Hutool 默认的安全限制是压缩比至少为 0.01,即压缩后的文件大小至少应为解压后文件大小的 1%。在这个例子中,压缩比低于此阈值,因此触发了安全检查并抛出了异常。

主要是因为 Hutool 在处理 Excel 文件时检测到了所谓的“Zip bomb”。这是一种潜在的安全风险,因为压缩文件解压后的大小远大于其原始大小,可能导致内存耗尽或性能下降。

解决方案也比较简单,在打开文件之前加入一行

java">ZipSecureFile.setMinInflateRatio(0.009935);ExcelWriter writer ExcelUtil.getWriter("C:\\Users\\Breeze\\Desktop\\"+fileName+".xlsx");

即可解决!


http://www.ppmy.cn/server/108374.html

相关文章

C语言深度复习【数组和指针】

目录 一.数组和指针 1.1 数组指针 1.2 指针数组 1.3 函数指针 1.4 const和指针 1.5 sizeof和指针和数组 1.6 strlen和字符数组 一.数组和指针 1.1 数组指针 一个数组指针实际上是指指向数组的指针。当你有一个数组类型作为函数参数时&#xff0c;它在函数内部被当作一个…

oracle物理存储结构文件详解

文章目录 oracle物理文件结构图① 控制文件&#xff1a;② 数据文件&#xff1a;③ 联机Redo日志文件&#xff1a;④ 参数文件&#xff1a;⑤ 归档文件&#xff1a;⑥ 密码文件&#xff1a; oracle物理文件结构图 Oracle数据库的物理结构由控制文件&#xff08;Control f…

C#性能驱动的内存处理:使用 Span<T> 和 Memory<T> 提升代码效率

本文示例内容&#xff1a; 分割数据&#xff1a;示例中将一个整数数组分割成两个部分&#xff0c;展示了如何使用 Span<T>.Slice 方法来获取数组的不同切片。计算数据&#xff1a;计算数组每个部分的总和&#xff0c;展示了如何利用 Span<T> 来遍历和处理数据。修…

小阿轩yx-Kubernetes存储入门

小阿轩yx-Kubernetes存储入门 前言 数据是一个企业的发展核心&#xff0c;它涉及到数据存储和数据交换的内容。在生产环境中尤为重要的一部分在 Kubernetes 中另一个重要的概念就是数据持久化 Volume。 Volume 的概念 对多数项目而言 数据文件的存储是非常常见的 在 Kube…

VBA技术资料MF180:将某个文件夹中的某类图片导入Word

我给VBA的定义&#xff1a;VBA是个人小型自动化处理的有效工具。利用好了&#xff0c;可以大大提高自己的工作效率&#xff0c;而且可以提高数据的准确度。“VBA语言専攻”提供的教程一共九套&#xff0c;分为初级、中级、高级三大部分&#xff0c;教程是对VBA的系统讲解&#…

备战秋招60天算法挑战,Day26

题目链接&#xff1a; https://leetcode.cn/problems/jump-game/ 视频题解&#xff1a; https://www.bilibili.com/video/BV1gwYKekEVN/ LeetCode 55. 跳跃游戏 题目描述 给定一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。 数组中的每个元素代表你在该…

python基础学习(最终篇)

文章目录 JSON的基础使用一. JSON简介二. JSON语法规则三. JSON数据类型四. JSON对象五. JSON数组六. JSON函数1. json.dumps2. json.loads3. json.dump4. json.load5. encode6. decode7. 参数说明 总结 JSON的基础使用 一. JSON简介 JSON(JavaScript Object Notation) 是一种…

比特币牛市将至背后

作者&#xff1a;Arthur Hayes 编译&#xff1a;Liam 「此处所表达的任何观点均为作者个人意见&#xff0c;不应作为投资决策依据&#xff0c;也不应被视为参与投资交易的推荐或建议。」 我打破常规&#xff0c;前往南半球滑雪两周&#xff0c;为北半球的暑假画上圆满的句号。我…