Просмотр исходного кода

增加ZIP工具类指定字符集构造方法;强制指定ZIP格式路径分隔符

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 2 лет назад
Родитель
Сommit
f334c14394

+ 2 - 0
tools-common/src/main/java/com/qmth/boot/tools/io/IOUtils.java

@@ -7,6 +7,8 @@ import java.nio.file.StandardCopyOption;
 
 public class IOUtils {
 
+    public static final String ZIP_SEPERATOR = "/";
+
     public static void closeQuietly(FileChannel fc) {
         try {
             if (fc != null) {

+ 11 - 4
tools-common/src/main/java/com/qmth/boot/tools/io/ZipReader.java

@@ -25,7 +25,7 @@ public class ZipReader {
     private Node root;
 
     /**
-     * 只支持从本地文件初始化,默认使用UTF8字符集
+     * 从本地文件初始化,默认使用UTF8字符集
      *
      * @param file
      * @throws IOException
@@ -34,6 +34,13 @@ public class ZipReader {
         this(file, StandardCharsets.UTF_8);
     }
 
+    /**
+     * 从本地文件初始化,指定字符集
+     *
+     * @param file
+     * @param charset
+     * @throws IOException
+     */
     public ZipReader(File file, Charset charset) throws IOException {
         if (file == null) {
             throw new IllegalArgumentException("file should not be null");
@@ -44,7 +51,7 @@ public class ZipReader {
         this.root = new Node();
         this.zipFile = new ZipFile(file, charset);
         zipFile.stream().forEach(entry -> {
-            String[] paths = entry.getName().split(File.separator);
+            String[] paths = entry.getName().split(IOUtils.ZIP_SEPERATOR);
             int size = paths.length;
             Node current = root;
             for (int i = 0; i < (size - 1); i++) {
@@ -94,7 +101,7 @@ public class ZipReader {
      * @throws IOException
      */
     public InputStream read(String... path) throws IOException {
-        ZipEntry entry = zipFile.getEntry(StringUtils.join(path, File.separator));
+        ZipEntry entry = zipFile.getEntry(StringUtils.join(path, IOUtils.ZIP_SEPERATOR));
         if (entry != null) {
             return zipFile.getInputStream(entry);
         } else {
@@ -121,7 +128,7 @@ public class ZipReader {
     }
 
     public static void main(String[] args) throws IOException {
-        ZipReader reader = new ZipReader(new File("/Users/luoshi/Downloads/test.zip"));
+        ZipReader reader = new ZipReader(new File("/Users/luoshi/Downloads/data.zip"));
         System.out.println(reader.list(false));
         reader.close();
     }

+ 32 - 6
tools-common/src/main/java/com/qmth/boot/tools/io/ZipWriter.java

@@ -3,6 +3,8 @@ package com.qmth.boot.tools.io;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.*;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
@@ -14,17 +16,28 @@ public class ZipWriter {
     private ZipOutputStream ous;
 
     /**
-     * 创建输出到通用输出流的ZIP内容
+     * 创建输出到通用输出流的ZIP内容,默认使用UTF8
      *
      * @param outputStream
      * @return
      */
     public static ZipWriter create(OutputStream outputStream) {
-        return new ZipWriter(outputStream);
+        return new ZipWriter(outputStream, StandardCharsets.UTF_8);
     }
 
     /**
-     * 创建输出到本地文件的ZIP内容
+     * 创建输出到通用输出流的ZIP内容,指定字符集
+     *
+     * @param outputStream
+     * @param charset
+     * @return
+     */
+    public static ZipWriter create(OutputStream outputStream, Charset charset) {
+        return new ZipWriter(outputStream, charset);
+    }
+
+    /**
+     * 创建输出到本地文件的ZIP内容,默认使用UTF8
      *
      * @param file
      * @return
@@ -33,11 +46,24 @@ public class ZipWriter {
         return create(new FileOutputStream(file));
     }
 
-    private ZipWriter(OutputStream outputStream) {
+    /**
+     * 创建输出到本地文件的ZIP内容,指定字符集
+     *
+     * @param file
+     * @return
+     */
+    public static ZipWriter create(File file, Charset charset) throws FileNotFoundException {
+        return create(new FileOutputStream(file), charset);
+    }
+
+    private ZipWriter(OutputStream outputStream, Charset charset) {
         if (outputStream == null) {
             throw new IllegalArgumentException("OutputStream should not be null");
         }
-        this.ous = new ZipOutputStream(outputStream);
+        if (charset == null) {
+            throw new IllegalArgumentException("Charset should not be null");
+        }
+        this.ous = new ZipOutputStream(outputStream, charset);
     }
 
     /**
@@ -54,7 +80,7 @@ public class ZipWriter {
         if (path == null || path.length == 0) {
             throw new IllegalArgumentException("path should not be empty");
         }
-        ous.putNextEntry(new ZipEntry(StringUtils.join(path, File.separator)));
+        ous.putNextEntry(new ZipEntry(StringUtils.join(path, IOUtils.ZIP_SEPERATOR)));
         IOUtils.copy(inputStream, ous);
         IOUtils.closeQuietly(inputStream);
     }