mirror of
				https://gitee.com/dromara/hutool.git
				synced 2025-11-01 00:46:56 +08:00 
			
		
		
		
	ZipUtil.unzip增加编码容错(issue#I3UZ28@Gitee)
This commit is contained in:
		| @@ -2,10 +2,11 @@ | ||||
| # 🚀Changelog | ||||
|  | ||||
| ------------------------------------------------------------------------------------------------------------- | ||||
| # 5.8.34(2024-11-12) | ||||
| # 5.8.34(2024-11-13) | ||||
|  | ||||
| ### 🐣新特性 | ||||
| * 【http   】      增加Windows微信浏览器识别(issue#IB3SJF@Gitee) | ||||
| * 【core   】      ZipUtil.unzip增加编码容错(issue#I3UZ28@Gitee) | ||||
|  | ||||
| ### 🐞Bug修复 | ||||
| * 【core   】      修复DateUtil.rangeToList中step小于等于0时无限循环问题(issue#3783@Github) | ||||
|   | ||||
| @@ -34,10 +34,7 @@ import java.util.ArrayList; | ||||
| import java.util.Enumeration; | ||||
| import java.util.List; | ||||
| import java.util.function.Consumer; | ||||
| import java.util.zip.ZipEntry; | ||||
| import java.util.zip.ZipFile; | ||||
| import java.util.zip.ZipInputStream; | ||||
| import java.util.zip.ZipOutputStream; | ||||
| import java.util.zip.*; | ||||
|  | ||||
| /** | ||||
|  * 压缩工具类 | ||||
| @@ -65,6 +62,17 @@ public class ZipUtil { | ||||
| 		try { | ||||
| 			return new ZipFile(file, ObjectUtil.defaultIfNull(charset, CharsetUtil.CHARSET_UTF_8)); | ||||
| 		} catch (IOException e) { | ||||
| 			// issue#I3UZ28 可能编码错误提示 | ||||
| 			if(e instanceof ZipException){ | ||||
| 				if(e.getMessage().contains("invalid CEN header")){ | ||||
| 					try { | ||||
| 						// 尝试使用不同编码 | ||||
| 						return new ZipFile(file, CharsetUtil.CHARSET_UTF_8.equals(charset) ? CharsetUtil.CHARSET_GBK : CharsetUtil.CHARSET_UTF_8); | ||||
| 					} catch (final IOException ex) { | ||||
| 						throw new IORuntimeException(ex); | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 			throw new IORuntimeException(e); | ||||
| 		} | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Looly
					Looly