|
@@ -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);
|
|
|
}
|