Skip to content

Instantly share code, notes, and snippets.

@pruidong
Last active April 25, 2022 03:42
Show Gist options
  • Save pruidong/9d1c60a28ab266111bb438753e21cca1 to your computer and use it in GitHub Desktop.
Save pruidong/9d1c60a28ab266111bb438753e21cca1 to your computer and use it in GitHub Desktop.
Java生成多级ZIP压缩文件
// log: 使用Lombok的@Slf4j.
/**
*
* 获取zip文件
*
*/
@GetMapping("/get-zip")
public void getZip(
HttpServletResponse response) {
String fileName = "xxx_file";
byte[] data = genData();
try {
genZip(response, data, fileName);
} catch (IOException e) {
log.error(
"发生异常,异常信息: {}",
e.getMessage());
e.printStackTrace();
}
}
/**
* 生成数据.
*
*/
public byte[] genData() throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(outputStream);
try{
// 给压缩文件中添加多个文件,可以设置文件名.
Collections.emptyList().forEach(
item -> {
Random random=new Random();
int id=random.nextInt();
String fileName =
item + "_" + id + ".json";
try {
// 设置文件名
zipOut.putNextEntry(new ZipEntry(fileName));
// 添加文件内容
zipOut.write(fileName.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
log.error(
"获取数据失败,失败原因: {}",
e.getMessage());
e.printStackTrace();
}
});
} finally {
try {
zipOut.closeEntry();
zipOut.close();
outputStream.close();
} catch (IOException e) {
log.error(
"关闭数据压缩失败,失败原因: {}",
e.getMessage());
e.printStackTrace();
}
}
return outputStream.toByteArray();
}
/**
* 压缩文件输出.
*
* @param response 响应
* @param data 数据
* @param fileName 文件名
* @throws IOException IO异常
*/
private void outZip(HttpServletResponse response, byte[] data, String fileName)
throws IOException {
response.reset();
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".zip\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
org.apache.commons.io.IOUtils.write(data, response.getOutputStream());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment