java生成zip文件,zip文件分两种:一种是包含文件夹、一种是不包含文件夹
生成zip文件的方式
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
// 文件夹名称
String folder = "download/";
ZipEntry ze = new ZipEntry(folder);
zipOutputStream.putNextEntry(ze);
ze = new ZipEntry(folder + "test.xlsx");
zipOutputStream.putNextEntry(ze);
// 读取文件内容并写入ZIP文件中
FileInputStream fis = new FileInputStream(new File(excelName));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
fis.close();
zipOutputStream.closeEntry();
zipOutputStream.close();