diff --git a/CHANGELOG.md b/CHANGELOG.md
index 12ec763a5..91653efa1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@
### 🐣新特性
* 【core 】 MapUtil增加entry、ofEntries方法
+* 【core 】 ZipWriter增加add方法重载
### 🐞Bug修复
* 【core 】 IdcardUtil#getCityCodeByIdCard位数问题(issue#2224@Github)
diff --git a/hutool-core/src/main/java/cn/hutool/core/compress/ZipWriter.java b/hutool-core/src/main/java/cn/hutool/core/compress/ZipWriter.java
index b27d86338..fef99f062 100755
--- a/hutool-core/src/main/java/cn/hutool/core/compress/ZipWriter.java
+++ b/hutool-core/src/main/java/cn/hutool/core/compress/ZipWriter.java
@@ -6,6 +6,7 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
+import cn.hutool.core.util.ZipUtil;
import java.io.Closeable;
import java.io.File;
@@ -66,7 +67,7 @@ public class ZipWriter implements Closeable {
* @param charset 编码
*/
public ZipWriter(OutputStream out, Charset charset) {
- this.out = getZipOutputStream(out, charset);
+ this.out = ZipUtil.getZipOutputStream(out, charset);
}
/**
@@ -176,6 +177,31 @@ public class ZipWriter implements Closeable {
return putEntry(path, in);
}
+ /**
+ * 对流中的数据加入到压缩文件
+ * 路径列表和流列表长度必须一致
+ *
+ * @param paths 流数据在压缩文件中的路径或文件名
+ * @param ins 要压缩的源,添加完成后自动关闭流
+ * @return 压缩文件
+ * @throws IORuntimeException IO异常
+ * @since 5.8.0
+ */
+ public ZipWriter add(String[] paths, InputStream[] ins) throws IORuntimeException {
+ if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
+ throw new IllegalArgumentException("Paths or ins is empty !");
+ }
+ if (paths.length != ins.length) {
+ throw new IllegalArgumentException("Paths length is not equals to ins length !");
+ }
+
+ for (int i = 0; i < paths.length; i++) {
+ add(paths[i], ins[i]);
+ }
+
+ return this;
+ }
+
@Override
public void close() throws IORuntimeException {
try {
@@ -195,21 +221,7 @@ public class ZipWriter implements Closeable {
* @return {@link ZipOutputStream}
*/
private static ZipOutputStream getZipOutputStream(File zipFile, Charset charset) {
- return getZipOutputStream(FileUtil.getOutputStream(zipFile), charset);
- }
-
- /**
- * 获得 {@link ZipOutputStream}
- *
- * @param out 压缩文件流
- * @param charset 编码
- * @return {@link ZipOutputStream}
- */
- private static ZipOutputStream getZipOutputStream(OutputStream out, Charset charset) {
- if (out instanceof ZipOutputStream) {
- return (ZipOutputStream) out;
- }
- return new ZipOutputStream(out, charset);
+ return ZipUtil.getZipOutputStream(FileUtil.getOutputStream(zipFile), charset);
}
/**
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 84657cfd1..315d2f826 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
@@ -83,6 +83,21 @@ public class ZipUtil {
}
}
+ /**
+ * 获得 {@link ZipOutputStream}
+ *
+ * @param out 压缩文件流
+ * @param charset 编码
+ * @return {@link ZipOutputStream}
+ * @since 5.8.0
+ */
+ public static ZipOutputStream getZipOutputStream(OutputStream out, Charset charset) {
+ if (out instanceof ZipOutputStream) {
+ return (ZipOutputStream) out;
+ }
+ return new ZipOutputStream(out, charset);
+ }
+
/**
* 在zip文件中添加新文件或目录
* 新文件添加在zip根目录,文件夹包括其本身和内容
@@ -370,17 +385,8 @@ public class ZipUtil {
* @since 3.0.9
*/
public static File zip(File zipFile, String[] paths, InputStream[] ins, Charset charset) throws UtilException {
- if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
- throw new IllegalArgumentException("Paths or ins is empty !");
- }
- if (paths.length != ins.length) {
- throw new IllegalArgumentException("Paths length is not equals to ins length !");
- }
-
try (final ZipWriter zipWriter = ZipWriter.of(zipFile, charset)) {
- for (int i = 0; i < paths.length; i++) {
- zipWriter.add(paths[i], ins[i]);
- }
+ zipWriter.add(paths, ins);
}
return zipFile;
@@ -395,18 +401,7 @@ public class ZipUtil {
* @since 5.5.2
*/
public static void zip(OutputStream out, String[] paths, InputStream[] ins) {
- if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
- throw new IllegalArgumentException("Paths or ins is empty !");
- }
- if (paths.length != ins.length) {
- throw new IllegalArgumentException("Paths length is not equals to ins length !");
- }
-
- try (final ZipWriter zipWriter = ZipWriter.of(out, DEFAULT_CHARSET)) {
- for (int i = 0; i < paths.length; i++) {
- zipWriter.add(paths[i], ins[i]);
- }
- }
+ zip(getZipOutputStream(out, DEFAULT_CHARSET), paths, ins);
}
/**
@@ -419,17 +414,8 @@ public class ZipUtil {
* @since 5.5.2
*/
public static void zip(ZipOutputStream zipOutputStream, String[] paths, InputStream[] ins) throws IORuntimeException {
- if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
- throw new IllegalArgumentException("Paths or ins is empty !");
- }
- if (paths.length != ins.length) {
- throw new IllegalArgumentException("Paths length is not equals to ins length !");
- }
-
try (final ZipWriter zipWriter = new ZipWriter(zipOutputStream)) {
- for (int i = 0; i < paths.length; i++) {
- zipWriter.add(paths[i], ins[i]);
- }
+ zipWriter.add(paths, ins);
}
}