From 490e5129754a908ebff55c04b62b04c85d36d558 Mon Sep 17 00:00:00 2001 From: looly Date: Sat, 20 Nov 2021 17:08:28 +0800 Subject: [PATCH] fix bug --- CHANGELOG.md | 3 ++- .../src/main/java/cn/hutool/core/util/ZipUtil.java | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c1df9dd0..a0f10bfb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.17 (2021-11-19) +# 5.7.17 (2021-11-20) ### 🐣新特性 * 【core 】 增加AsyncUtil(pr#457@Gitee) @@ -24,6 +24,7 @@ ### 🐞Bug修复 * 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github) * 【cache 】 修复WeakCache键值强关联导致的无法回收问题(issue#1953@Github) +* 【core 】 修复ZipUtil相对路径父路径获取null问题(issue#1961@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java index ddce403fb..3b1ac5655 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java @@ -1002,8 +1002,17 @@ public class ZipUtil { throw new UtilException(StrUtil.format("File [{}] not exist!", srcFile.getAbsolutePath())); } + // issue#1961@Github + // 当 zipFile = new File("temp.zip") 时, zipFile.getParentFile() == null + File parentFile; + try { + parentFile = zipFile.getCanonicalFile().getParentFile(); + } catch (IOException e) { + parentFile = zipFile.getParentFile(); + } + // 压缩文件不能位于被压缩的目录内 - if (srcFile.isDirectory() && FileUtil.isSub(srcFile, zipFile.getParentFile())) { + if (srcFile.isDirectory() && FileUtil.isSub(srcFile, parentFile)) { throw new UtilException("Zip file path [{}] must not be the child directory of [{}] !", zipFile.getPath(), srcFile.getPath()); } }