使用 ZipOutputStream 压缩包下载
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
entity.stream().forEach(file-> {
try {
zip.putNextEntry(new ZipEntry(fileName));
File fileObj = new File(path);
byte[] bytes = Files.readAllBytes(fileObj.toPath());
zip.write(bytes);
zip.flush();
zip.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
});
IOUtils.closeQuietly(zip);
IOUtils.write(outputStream.toByteArray(), response.getOutputStream());