mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
ZipUtil.unzip增加编码容错(issue#I3UZ28@Gitee)
This commit is contained in:
parent
d88eac0dac
commit
9b3709ad45
@ -33,7 +33,6 @@ import org.dromara.hutool.core.text.CharUtil;
|
|||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -45,10 +44,7 @@ import java.util.Enumeration;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.*;
|
||||||
import java.util.zip.ZipFile;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
import java.util.zip.ZipOutputStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 压缩工具类
|
* 压缩工具类
|
||||||
@ -72,10 +68,24 @@ public class ZipUtil {
|
|||||||
* @param charset 解析zip文件的编码,null表示{@link CharsetUtil#UTF_8}
|
* @param charset 解析zip文件的编码,null表示{@link CharsetUtil#UTF_8}
|
||||||
* @return {@link ZipFile}
|
* @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 {
|
try {
|
||||||
return new ZipFile(file, ObjUtil.defaultIfNull(charset, CharsetUtil.UTF_8));
|
return new ZipFile(file, charset);
|
||||||
} catch (final IOException e) {
|
} 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);
|
throw new IORuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user