package util;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
public static void zipFiles(List<String> fileList, String localPath,String zipFileName, HttpServletResponse response){
OutputStream out = null;
String zipPathName = localPath+"/"+zipFileName;
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(zipPathName)))){
File zipFile = new File(zipPathName);
if (!zipFile.exists()){
zipFile.mkdirs();
}
for (String filePathName:fileList){
File file = new File(filePathName);
if (file.exists()){
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[2048];
compressSingleFile(file,zos,buffer);
}
}
//响应到浏览器
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment;filename="+zipFileName);
out = response.getOutputStream();
byte[] bytes = readFileToBytes(new File(zipPathName));
out.write(bytes);
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (out!=null){
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
public static byte[] readFileToBytes(File file) throws IOException {
FileInputStream fis = null;
if (file.exists()){
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int len;
while ((len= bis.read(buffer,0, buffer.length))!=-1){
baos.write(buffer,0,len);
}
bis.close();
fis.close();
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fis!=null){
fis.close();
}
}
}else {
return null;
}
}
/**
* 压缩单文件
* @param file
* @param zos
* @param buffer
*/
public static void compressSingleFile(File file,ZipOutputStream zos,byte[] buffer){
int len;
try(FileInputStream fis = new FileInputStream(file)){
while ((len=fis.read(buffer))>0){
zos.write(buffer,0,len);
zos.flush();
}
zos.closeEntry();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
参考:java 生成zip文件并浏览器导出 - 爱码网 (likecs.com)https://www.likecs.com/show-840847.html(47条消息) java实现多文件压缩_yuhuofei2021的博客-CSDN博客_java 压缩多个文件
https://blog.csdn.net/Crezfikbd/article/details/126268756以上版本为未优化版本会在本地留存压缩文件
优化后的版本为:
public static void zipFiles(List<String> fileList, String zipFileName, HttpServletResponse response) throws IOException {
byte[] buf = new byte[1024];
BufferedOutputStream bos = null;
ZipOutputStream out = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
//响应到浏览器
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment;filename="+zipFileName);
out = new ZipOutputStream(bos);
for (String filePathName:fileList){
File file = new File(filePathName);
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
int len = -1;
while ((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (out!=null)
out.close();
if (bos!=null)
bos.close();
}
}