Bladeren bron

修改ZipWriter接口,强制添加本地文件时,必须提供ZIP内部文件名,不再默认取本地文件名

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 3 jaren geleden
bovenliggende
commit
e3a19fc78b

+ 4 - 15
tools-common/src/main/java/com/qmth/boot/tools/io/ZipWriter.java

@@ -1,8 +1,6 @@
 package com.qmth.boot.tools.io;
 
 import com.sun.istack.internal.NotNull;
-import com.sun.istack.internal.Nullable;
-import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.*;
@@ -66,23 +64,14 @@ public class ZipWriter {
     }
 
     /**
-     * 使用本地文件增加ZIP内部文件,dir表示完整的内部路径,可以为空;不指定内部文件名时,直接使用本地文件名
+     * 使用本地文件增加ZIP内部文件,path包括完整的内部路径,至少包含内部文件名
      *
      * @param file
-     * @param name
-     * @param dir
+     * @param path
      * @throws IOException
      */
-    public void write(@NotNull File file, @Nullable String name, @Nullable String... dir) throws IOException {
-        if (name == null) {
-            name = file.getName();
-        }
-        if (dir == null || dir.length == 0) {
-            dir = new String[] { name };
-        } else {
-            dir = ArrayUtils.add(dir, name);
-        }
-        write(new FileInputStream(file), dir);
+    public void write(@NotNull File file, @NotNull String... path) throws IOException {
+        write(new FileInputStream(file), path);
     }
 
     /**

+ 2 - 2
tools-common/src/test/java/com/qmth/boot/test/tools/zip/ZipWriterTest.java

@@ -11,8 +11,8 @@ public class ZipWriterTest {
     public void test() throws IOException {
         ZipWriter writer = ZipWriter.create(new File("/Users/luoshi/Downloads/test.zip"));
         writer.write("123".getBytes(), "123.txt");
-        writer.write("456".getBytes(), "dir1", "文本.txt");
-        writer.write(new File("/Users/luoshi/Downloads/cccz.png"), null, "dir2");
+        writer.write("文本内容".getBytes(), "dir1", "文本.txt");
+        writer.write(new File("/Users/luoshi/Downloads/cccz.png"), "目录2", "cccz.png");
         writer.write(new File("/Users/luoshi/Downloads/ncu.png"), "图片.png");
         writer.close();
     }