MultipartFile 类型文件批量压缩到一个zip
private String mkZip(MultipartFile[] targetFiles) throws IOException {
List<File> files=new ArrayList<>();
for (MultipartFile m:targetFiles) {
String dateString = DateUtil.getDateString(new Date(), DateUtil.DATE_TIME_PATTERN);
File file = new File("C:/temp", dateString + m.getOriginalFilename());
file.setWritable(true, false);
m.transferTo(file);
files.add(file);
}
File zip = new File("C:/temp/"+System.currentTimeMillis() + ".zip");
zip.setWritable(true, false);
zip.createNewFile();
String fileFolders = zipFileFolders(files, zip);
return fileFolders ;
}
public String zipFileFolders(List<File> srcfile, File zipfile) {
byte[] buf = new byte[1024];
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipfile));
for (int i = 0; i < srcfile.size(); i++) {
FileInputStream in = new FileInputStream(srcfile.get(i));
String filePath="";
if (filePath == null)
filePath = "";
else
filePath += "/";
out.putNextEntry(new ZipEntry(filePath + srcfile.get(i).getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
return zipfile.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}