ZipUtil.java
package com.hs.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.lang3.StringUtils;
public class ZipUtil {
/**
* 压缩文件列表中的文件
*
* @param files
* @param outputStream
* @throws IOException
*/
public static void zipFile(List<String> fileNameList,List<File> files, ZipOutputStream outputStream) throws IOException, ServletException {
try {
int size = files.size();
//压缩列表中的文件
for (int i = 0; i < size; i++) {
File file = (File) files.get(i);
String fileName = fileNameList.get(i);
try {
zipFile(fileName,file, outputStream);
} catch (Exception e) {
continue;
}
}
} catch (Exception e) {
throw e;
}
}/**
* 将文件写入到zip文件中
*
* @param inputFile
* @param outputstream
* @throws Exception
*/
public static void zipFile(String fileName, File inputFile, ZipOutputStream outputstream) throws IOException, ServletException {
try {
if (inputFile.exists()) {
if (inputFile.isFile()) {
FileInputStream inStream = new FileInputStream(inputFile);
BufferedInputStream bInStream = new BufferedInputStream(inStream);
ZipEntry entry = new ZipEntry(fileName);
outputstream.putNextEntry(entry);final int MAX_BYTE = 10 * 1024 * 1024; //最大的流为10M
long streamTotal = 0; //接受流的容量
int streamNum = 0; //流需要分开的数量
int leaveByte = 0; //文件剩下的字符数
byte[] inOutbyte; //byte数组接受文件的数据streamTotal = bInStream.available(); //通过available方法取得流的最大字符数
streamNum = (int) Math.floor(streamTotal / MAX_BYTE); //取得流文件需要分开的数量
leaveByte = (int) streamTotal % MAX_BYTE; //分开文件之后,剩余的数量if (streamNum > 0) {
for (int j = 0; j < streamNum; ++j) {
inOutbyte = new byte[MAX_BYTE];
//读入流,保存在byte数组
bInStream.read(inOutbyte, 0, MAX_BYTE);
outputstream.write(inOutbyte, 0, MAX_BYTE); //写出流
}
}
//写出剩下的流数据
inOutbyte = new byte[leaveByte];
bInStream.read(inOutbyte, 0, leaveByte);
outputstream.write(inOutbyte);
outputstream.closeEntry(); //Closes the current ZIP entry and positions the stream for writing the next entry
bInStream.close(); //关闭
inStream.close();
}
} else {
throw new ServletException("文件不存在!");
}
} catch (IOException e) {
throw e;
}
}/**
* 下载打包的文件
*
* @param file
* @param response
*/
public static void downloadZip(File file, HttpServletResponse response ,HttpServletRequest request) {
try {
// 以流的形式下载文件。
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
String fileName = file.getName().replace(" ", "");
fileName=new String(fileName.getBytes("utf-8"),"iso-8859-1");
fileName = proccessFileName(request,fileName);
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
toClient.write(buffer);
toClient.flush();
toClient.close();
file.delete(); //将生成的服务器端文件删除
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
*
* @Title proccessFileName下载文件的乱码问题
* @author lize16713
* @param @param request
* @param @param fileName 要下载文件名
* @return String 处理好的文件名
*/
public static String proccessFileName(HttpServletRequest request, String fileName) {
String codedFileName = null;
try {
String userAgent = request.getHeader("User-Agent");
if (StringUtils.isNotEmpty(userAgent) && (-1 != userAgent.indexOf("Edge")
||-1 != userAgent.indexOf("MSIE")|| -1 != userAgent.indexOf("Trident"))) {// IE
codedFileName = java.net.URLEncoder.encode(fileName, "ISO-8859-1");
} else if (StringUtils.isNotEmpty(userAgent) && -1 != userAgent.indexOf("Mozilla")) {
codedFileName = fileName;
}
} catch (Exception e) {
e.printStackTrace();
}
return codedFileName;
}}
根据实际情况修改函数batchDownload
/**
* 批量打包下载<br>
* @author: <br>
* @since: 2018年9月4日 <br>
* 修改日期 修改人员 修改说明 <br>
*/
public void batchDownload(HttpServletRequest request,HttpServletResponse response, String ids)throws Exception{
String[] file_ids = ids.split(",");
//压缩文件
List<String> fileNameList = new ArrayList<>();
List<File> files = new ArrayList<>();
for (int i=0;i<file_ids.length;i++) {
String id = this.findById(file_ids[i]).getManusfile_ids();
SysDynAttachmentForm sysDynAttachmentForm= sysDynAttachmentLogicImp.findById(id);
String path = fileDiskPath + sysDynAttachmentForm.getFile_path();
String name = sysDynAttachmentForm.getFile_name();
File file = new File(path);
files.add(file);
fileNameList.add(name);
}
String fileName = "底稿下载.zip";
String outFilePath = "d:\\" + fileName;
File file = new File(outFilePath);
//文件输出流
FileOutputStream outStream = new FileOutputStream(file);
//压缩流
ZipOutputStream toClient = new ZipOutputStream(outStream);
// toClient.setEncoding("GBK");
ZipUtil.zipFile(fileNameList,files, toClient);
toClient.close();
outStream.close();
ZipUtil.downloadZip(file, response, request);
}