重点在于
zos.putNextEntry(new ZipEntry(entry.getKey() + File.separator + file.getName()));
这一行代码。使用entry.getKey()用来作为文件夹名,File.separator是作为分隔符(windows是 \ )
以下为项目中使用的完整代码。
public void zipMultipleFiles(Map<String, List<File>> files, HttpServletResponse response) {
String downloadFilename = SDF.format(new Date());
ZipOutputStream zos = null;
InputStream in = null;
List<String> fileNameList = new ArrayList<>();
try {
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
zos = new ZipOutputStream(respon