|
|
@@ -0,0 +1,60 @@
|
|
|
+package com.common.workflow.service.util;
|
|
|
+
|
|
|
+import com.common.workflow.service.dto.FileDto;
|
|
|
+import net.lingala.zip4j.core.ZipFile;
|
|
|
+import net.lingala.zip4j.exception.ZipException;
|
|
|
+import net.lingala.zip4j.model.ZipParameters;
|
|
|
+import net.lingala.zip4j.util.Zip4jConstants;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j
|
|
|
+ * https://www.jianshu.com/p/89bf65317e6b
|
|
|
+ * @Author baiChFei
|
|
|
+ * @Email 738983525@qq.com
|
|
|
+ * @Date 2021/8/12 下午7:55
|
|
|
+ */
|
|
|
+public class File2Zip {
|
|
|
+ public static void fileZip(List<FileDto> fileUrls, String zipName, HttpServletResponse response) {
|
|
|
+ String zipUrl = "./" + zipName;
|
|
|
+ for (FileDto fileDto : fileUrls) {
|
|
|
+ addFileZip(fileDto.getUrl(), fileDto.getName(), zipUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 下载
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void addFileZip(String fileUrl, String fileName, String zipAddr) {
|
|
|
+ InputStream fileInputStream = null;
|
|
|
+ try {
|
|
|
+ URL url = new URL(fileUrl);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
|
+ //得到输入流
|
|
|
+ fileInputStream = conn.getInputStream();
|
|
|
+
|
|
|
+ ZipFile zip = new ZipFile(zipAddr);
|
|
|
+ ZipParameters para = new ZipParameters();
|
|
|
+ para.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
+ para.setFileNameInZip(fileName);
|
|
|
+ para.setSourceExternalStream(true);
|
|
|
+ // 以流的方式添加文件
|
|
|
+ zip.addStream(fileInputStream, para);
|
|
|
+ } catch (IOException | ZipException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ assert fileInputStream != null;
|
|
|
+ fileInputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|