1.压缩zip文件
import java.io.*;
import org.apache.tools.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.springframework.core.io.FileSystemResource;
/**
* 压缩zip文件
*
* @param List<Attachments> list
* @return
* @throws IOException
*/
public String zipFile(List<Attachments> list) throws IOException {
//默认文件存放地址
String rootPath = "D:\\";
String path = rootPath +"ZIP"+"test.zip";
//遍历获取表中文件路径
List<String> filePath = list.stream().map(e->e.getPath()).collect(Collectors.toList());
//创建zip文件
File file = new File(path);
//判断文件父目录是否存在
if(!file.getParentFile().exists()){
//父目录不存在则创建对应的父目录
file.getParentFile().mkdirs();
}
//读取文件
FileOutputStream inputStream = new FileOutputStream(path);
//输出流
ZipOutputStream out = new ZipOutputStream(inputStream);
//批量获取需要下载的文件
for(int i=0;i<filePath.size();i++){
File downloadFile = new File(rootPath+filePath.get(i));
//判断需要下载的文件是否存在
if(!downloadFile.exists()){
//创建父目录
downloadFile.getParentFile().mkdirs();
//创建新文件
downloadFile.createNewFile();
}
//设置输出流字符编码
out.setEncoding("UTF-8");
//获取数据库中文件的路径和文件名,放入zip