mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
zip压缩到本目录时可能造成的死循环问题
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user