|
@@ -0,0 +1,156 @@
|
|
|
+package com.github.zuihou.business.util;
|
|
|
+
|
|
|
+import com.github.zuihou.business.externalApi.entity.MesAttachment;
|
|
|
+import com.github.zuihou.file.entity.Attachment;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.util.List;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
+
|
|
|
+public class ZipFileUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * web下载打成压缩包的文件--流方式
|
|
|
+ *
|
|
|
+ * @param response 响应
|
|
|
+ * @param fileList 文件列表
|
|
|
+ * @param zipName 压缩包名
|
|
|
+ */
|
|
|
+ public static void downloadZipFiles(HttpServletResponse response, List<String> fileList, String zipName) {
|
|
|
+ ZipOutputStream zipOutputStream = null;
|
|
|
+ try {
|
|
|
+ //设置响应头
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ //设置文件名称
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + zipName);
|
|
|
+ zipOutputStream = new ZipOutputStream(response.getOutputStream());
|
|
|
+ for (String file : fileList) {
|
|
|
+ toZip(zipOutputStream, new File(file));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ //关闭资源
|
|
|
+ if (null != zipOutputStream) {
|
|
|
+ try {
|
|
|
+ zipOutputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩文件
|
|
|
+ *
|
|
|
+ * @param zipOutputStream 压缩文件流
|
|
|
+ * @param file 待压缩文件
|
|
|
+ */
|
|
|
+ private static void toZip(ZipOutputStream zipOutputStream, File file) {
|
|
|
+ String filename = file.getName();
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try {
|
|
|
+ bis = new BufferedInputStream(Files.newInputStream(file.toPath()));
|
|
|
+ //设置压缩包内文件的名称
|
|
|
+ zipOutputStream.putNextEntry(new ZipEntry(filename));
|
|
|
+ int size;
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ while ((size = bis.read(buffer)) > 0) {
|
|
|
+ zipOutputStream.write(buffer, 0, size);
|
|
|
+ }
|
|
|
+ zipOutputStream.closeEntry();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ //关闭资源
|
|
|
+ if (null != bis) {
|
|
|
+ try {
|
|
|
+ bis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量文件压缩下载
|
|
|
+ * @param urlList 需要批量下载文件的链接地址列表
|
|
|
+ * @param zipName 输出的压缩包名称
|
|
|
+ */
|
|
|
+ public static void downZip(List<MesAttachment> urlList, String zipName, HttpServletRequest request, HttpServletResponse response){
|
|
|
+ //响应头的设置
|
|
|
+ response.reset();
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentType("multipart/form-data");
|
|
|
+ String downloadName = zipName+".zip";
|
|
|
+ //返回客户端浏览器的版本号、类型
|
|
|
+ String agent = request.getHeader("USER-AGENT");
|
|
|
+ try {
|
|
|
+ //针对IE或者以IE为内核的浏览器:
|
|
|
+ if (agent.contains("MSIE")||agent.contains("Trident")) {
|
|
|
+ downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
|
|
|
+ } else {
|
|
|
+ //非IE浏览器的处理:
|
|
|
+ downloadName = new String(downloadName.getBytes("UTF-8"),"ISO-8859-1");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");
|
|
|
+
|
|
|
+ //设置压缩流:直接写入response,实现边压缩边下载
|
|
|
+ ZipOutputStream zipos = null;
|
|
|
+ try {
|
|
|
+ zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
|
|
|
+ zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //循环将文件写入压缩流
|
|
|
+ DataOutputStream os = null;
|
|
|
+
|
|
|
+ for (MesAttachment mesAttachment : urlList) {
|
|
|
+ try {
|
|
|
+ URL url = new URL(mesAttachment.getUrl());
|
|
|
+ URLConnection urlConnection = url.openConnection();
|
|
|
+ //File file = new File(mesAttachment.getUrl());
|
|
|
+ //此处应该加个文件是否存在的判断
|
|
|
+ String filename = mesAttachment.getName();
|
|
|
+ //添加ZipEntry,并ZipEntry中写入文件流
|
|
|
+ zipos.putNextEntry(new ZipEntry(filename));
|
|
|
+ os = new DataOutputStream(zipos);
|
|
|
+ InputStream is = urlConnection.getInputStream();
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ int length = 0;
|
|
|
+ while((length = is.read(b))!= -1){
|
|
|
+ os.write(b, 0, length);
|
|
|
+ }
|
|
|
+ is.close();
|
|
|
+ zipos.closeEntry();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //关闭流
|
|
|
+ try {
|
|
|
+ os.flush();
|
|
|
+ os.close();
|
|
|
+ zipos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|