zip压缩到本目录时可能造成的死循环问题

This commit is contained in:
Looly 2024-08-01 23:08:53 +08:00
parent 469b433b53
commit 02a38a4f52
3 changed files with 30 additions and 1 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.30(2024-07-31)
# 5.8.30(2024-08-01)
### 🐣新特性
* 【core 】 Converter转换规则变更空对象、空值转为Bean时创建默认对象而非nullissue#3649@Github
@ -26,6 +26,7 @@
* 【core 】 修复IdcardUtil.isValidHKCard校验问题issue#IAFOLI@Gitee
* 【core 】 修复Convert.digitToChinese(0)输出金额无`元整问题`issue#3662@Github
* 【core 】 修复CsvParser中对正文中双引号处理逻辑问题pr#1244@Gitee
* 【core 】 修复ZipUtil.zip压缩到本目录时可能造成的死循环问题issue#IAGYDG@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.29(2024-07-03)

View File

@ -48,6 +48,7 @@ public class ZipWriter implements Closeable {
return new ZipWriter(out, charset);
}
private File zipFile;
private final ZipOutputStream out;
/**
@ -57,6 +58,7 @@ public class ZipWriter implements Closeable {
* @param charset 编码
*/
public ZipWriter(File zipFile, Charset charset) {
this.zipFile = zipFile;
this.out = getZipOutputStream(zipFile, charset);
}
@ -254,6 +256,11 @@ public class ZipWriter implements Closeable {
}
}
} else {
// issue#IAGYDG 检查加入的文件是否为压缩结果文件本身避免死循环
if (FileUtil.equals(file, zipFile)) {
return this;
}
// 如果是文件或其它符号则直接压缩该文件
putEntry(subPath, FileUtil.getInputStream(file));
}

View File

@ -0,0 +1,21 @@
package cn.hutool.core.compress;
import cn.hutool.core.util.ZipUtil;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
/**
* https://gitee.com/dromara/hutool/issues/IAGYDG
*/
public class IssueIAGYDGTest {
@Test
@Ignore
public void zipTest() {
// 第一次压缩后IssueIAGYDG.zip也会作为文件压缩到IssueIAGYDG.zip中导致死循环
final File filea = new File("d:/test/");
final File fileb = new File("d:/test/IssueIAGYDG.zip");
ZipUtil.zip(fileb, false, filea.listFiles());
}
}