2023-05-08T17:56:56.901+08:00 ERROR 807978 --- [nio-5050-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: cn.hutool.core.io.IORuntimeException: ZipException: invalid CEN header (bad entry name)] with root cause
java.util.zip.ZipException: invalid CEN header (bad entry name)
at java.base/java.util.zip.ZipFile$Source.zerror(ZipFile.java:1598) ~[na:na]
at java.base/java.util.zip.ZipFile$Source.checkAndAddEntry(ZipFile.java:1209) ~[na:na]
at java.base/java.util.zip.ZipFile$Source.initCEN(ZipFile.java:1537) ~[na:na]
at java.base/java.util.zip.ZipFile$Source.<init>(ZipFile.java:1315) ~[na:na]
at java.base/java.util.zip.ZipFile$Source.get(ZipFile.java:1277) ~[na:na]
at java.base/java.util.zip.ZipFile$CleanableResource.<init>(ZipFile.java:709) ~[na:na]
at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:243) ~[na:na]
at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:296) ~[na:na]
at cn.hutool.core.util.ZipUtil.toZipFile(ZipUtil.java:56) ~[hutool-all-5.7.11.jar:na]
at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:513) ~[hutool-all-5.7.11.jar:na]
at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:500) ~[hutool-all-5.7.11.jar:na]
at com.fastonetech.billing.compress.CompressUtil.unzip(CompressUtil.kt:39) ~[main/:na]
at com.fastonetech.billing.scheduler.AuditHandler$initFastone$2.invokeSuspend(AuditHandler.kt:127) ~[main/:na]
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) ~[kotlin-stdlib-1.8.0.jar:1.8.0-release-345(1.8.0)]
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at com.fastonetech.billing.scheduler.AuditHandler.initFastone$suspendImpl(AuditHandler.kt:120) ~[main/:na]
at com.fastonetech.billing.scheduler.AuditHandler.initFastone(AuditHandler.kt) ~[main/:na]
at com.fastonetech.billing.scheduler.DataSetScheduler$loadBillData$2.invokeSuspend(DataSetScheduler.kt:69) ~[main/:na]
at com.fastonetech.billing.scheduler.DataSetScheduler$loadBillData$2.invoke(DataSetScheduler.kt) ~[main/:na]
at com.fastonetech.billing.scheduler.DataSetScheduler$loadBillData$2.invoke(DataSetScheduler.kt) ~[main/:na]
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:264) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at com.fastonetech.billing.scheduler.DataSetScheduler.loadBillData(DataSetScheduler.kt:55) ~[main/:na]
at com.fastonetech.billing.scheduler.DataSetScheduler.access$loadBillData(DataSetScheduler.kt:16) ~[main/:na]
at com.fastonetech.billing.scheduler.DataSetScheduler$loadBillingAuditUploadData$1.invokeSuspend(DataSetScheduler.kt:49) ~[main/:na]
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) ~[kotlin-stdlib-1.8.0.jar:1.8.0-release-345(1.8.0)]
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at com.fastonetech.billing.scheduler.DataSetScheduler.loadBillingAuditUploadData(DataSetScheduler.kt:45) ~[main/:na]
at com.fastonetech.billing.controller.DataSetController$fastLoadUpdate$1.invokeSuspend(DataSetController.kt:44) ~[main/:na]
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) ~[kotlin-stdlib-1.8.0.jar:1.8.0-release-345(1.8.0)]
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source) ~[kotlinx-coroutines-core-jvm-1.6.4.jar:na]
at com.fastonetech.billing.controller.DataSetController.fastLoadUpdate(DataSetController.kt:42) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:207) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:152) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1080) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:973) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) ~[spring-webmvc-6.0.4.jar:6.0.4]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:705) ~[tomcat-embed-core-10.1.5.jar:6.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.0.4.jar:6.0.4]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.springframework.web.servlet.resource.ResourceUrlEncodingFilter.doFilter(ResourceUrlEncodingFilter.java:66) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.springframework.web.filter.ServerHttpObservationFilter.doFilterInternal(ServerHttpObservationFilter.java:109) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
解决方案:
如果你在使用hutool库处理ZIP文件时遇到了java.util.zip.ZipException: invalid CEN header (bad entry name)异常,可以尝试以下方法解决问题:
确保ZIP文件完整且没有损坏:重新下载或重新创建ZIP文件。使用第三方ZIP工具(如7-Zip或WinRAR)检查和修复损坏的ZIP文件。
检查ZIP文件中的文件名:确保它们不包含非法字符,并使用支持的字符集(如UTF-8)。
在使用hutool解压ZIP文件时,指定正确的字符编码。例如,如果你知道ZIP文件使用UTF-8编码,可以在创建ZipUtil的实例时指定UTF-8编码:
import cn.hutool.core.util.ZipUtil;
import java.io.File;
public class HutoolUnzipExample {
public static void main(String[] args) {
File zipFile = new File("path/to/your/zipfile.zip");
File destDir = new File("path/to/destination/directory");
// 指定解压时使用的字符编码,例如:UTF-8
ZipUtil.unzip(zipFile, destDir, "UTF-8");
}
}
通过指定正确的字符编码,可以避免因编码问题导致的java.util.zip.ZipException: invalid CEN header (bad entry name)异常。
如果仍然无法解决问题,可以尝试使用其他Java库(如Apache Commons Compress)处理ZIP文件,以排除hutool库的潜在问题。
kotlin的解决代码
fun unzip(source: File, dest: File) {
ZipUtil.unzip(source, dest, CharsetUtil.CHARSET_GBK)
var success = tryUnzip(source, dest, CharsetUtil.CHARSET_UTF_8)
if (!success) {
success = tryUnzip(source, dest, CharsetUtil.CHARSET_GBK)
}
if(!success){
success = tryUnzip(source, dest, CharsetUtil.CHARSET_ISO_8859_1)
}
}
private fun tryUnzip(zipFile: File, destDir: File, charset: Charset): Boolean {
return try {
ZipUtil.unzip(zipFile, destDir, charset)
true
} catch (e: ZipException) {
logger.error("Failed to unzip with charset $charset: ${e.message}")
false
}
}
也可以使用这种方式实现
fun unzip(source: File, dest: File, charset: Charset = CharsetUtil.CHARSET_UTF_8) {
runCatching {
ZipUtil.unzip(source, dest, charset)
}.onFailure {
runCatching {
ZipUtil.unzip(source, dest, CharsetUtil.CHARSET_GBK)
}.onFailure {
runCatching {
ZipUtil.unzip(source, dest, CharsetUtil.CHARSET_ISO_8859_1)
}.exceptionOrNull()?.let {
logger.error("unzip ${source.name} failed!", it)
}
}
}
}