From 9b3709ad459d0553222e9ba02ecefc35eaad2650 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 13 Nov 2024 12:35:49 +0800 Subject: [PATCH] =?UTF-8?q?ZipUtil.unzip=E5=A2=9E=E5=8A=A0=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E5=AE=B9=E9=94=99=EF=BC=88issue#I3UZ28@Gitee=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/hutool/core/compress/ZipUtil.java | 24 +++++++++++++------ .../hutool/core/compress/IssueI3UZ28Test.java | 13 ++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 hutool-core/src/test/java/org/dromara/hutool/core/compress/IssueI3UZ28Test.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/compress/ZipUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/compress/ZipUtil.java index 2803f40e8..c0c65a30a 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/compress/ZipUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/compress/ZipUtil.java @@ -33,7 +33,6 @@ import org.dromara.hutool.core.text.CharUtil; import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.util.ByteUtil; import org.dromara.hutool.core.util.CharsetUtil; -import org.dromara.hutool.core.util.ObjUtil; import java.io.*; import java.nio.charset.Charset; @@ -45,10 +44,7 @@ import java.util.Enumeration; import java.util.List; import java.util.function.Consumer; import java.util.jar.JarFile; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; -import java.util.zip.ZipOutputStream; +import java.util.zip.*; /** * 压缩工具类 @@ -72,10 +68,24 @@ public class ZipUtil { * @param charset 解析zip文件的编码,null表示{@link CharsetUtil#UTF_8} * @return {@link ZipFile} */ - public static ZipFile toZipFile(final File file, final Charset charset) { + public static ZipFile toZipFile(final File file, Charset charset) { + if(null == charset){ + charset = CharsetUtil.UTF_8; + } try { - return new ZipFile(file, ObjUtil.defaultIfNull(charset, CharsetUtil.UTF_8)); + return new ZipFile(file, charset); } catch (final IOException e) { + // issue#I3UZ28 可能编码错误提示 + if(e instanceof ZipException){ + if(e.getMessage().contains("invalid CEN header")){ + try { + // 尝试使用不同编码 + return new ZipFile(file, CharsetUtil.UTF_8.equals(charset) ? CharsetUtil.GBK : CharsetUtil.UTF_8); + } catch (final IOException ex) { + throw new IORuntimeException(ex); + } + } + } throw new IORuntimeException(e); } } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/compress/IssueI3UZ28Test.java b/hutool-core/src/test/java/org/dromara/hutool/core/compress/IssueI3UZ28Test.java new file mode 100644 index 000000000..c51c6c1c0 --- /dev/null +++ b/hutool-core/src/test/java/org/dromara/hutool/core/compress/IssueI3UZ28Test.java @@ -0,0 +1,13 @@ +package org.dromara.hutool.core.compress; + +import org.dromara.hutool.core.io.file.FileUtil; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class IssueI3UZ28Test { + @Test + @Disabled + void unzipWithGBKTest() { + ZipUtil.unzip(FileUtil.file("d:/test/test.zip")); + } +}