1 Maven依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.2</version>
</dependency>
2 调试代码
/**
* 多个文件压缩成压缩包
*
* @param response
* @throws IOException
*/
@GetMapping("/multiFileToZip")
public void multiFileToZip(HttpServletResponse response) throws IOException {
//被压缩文件InputStream
InputStream[] srcFiles = new InputStream[]{new ClassPathResource("/doc/模板.docx").getInputStream()
, new ClassPathResource("/doc/模板合并单元格.docx").getInputStream()
, new ClassPathResource("/doc/模板填充文本.docx").getInputStream()
};
//被压缩文件名称
String[] srcFileNames = new String[]{"模板.docx", "模板合并单元格.docx", "模板填充文本.docx"};
//多个文件压缩成压缩包返回
ZipUtil.zip(response.getOutputStream(), srcFileNames, srcFiles);
}
3 模板文件
模板文件地址: