多个文件进行压缩
/**
* 将文件字节数组压缩
*
* @param fileByteArrayList 文件内容
* @return zip包文件内容
*/
public byte[] getZipByteArray(List<byte[]> fileByteArrayList) {
ByteArrayOutputStream zipContentStream = null;
ZipOutputStream zos = null;
try {
//初始化结果流
zipContentStream = new ByteArrayOutputStream();
zos = new ZipOutputStream(new BufferedOutputStream(zipContentStream));
//设置压缩级别
zos.setLevel(8);
for (byte[] fileByteArray : fileByteArrayList) {
//获取文件名称
String fileNameStr = "文件名称" + System.currentTimeMillis() + ".xls";
//zip文件中写入文件内容
zos.putNextEntry(new ZipEntry(fileNameStr));
zos.write(fileByteArray);
}
} catch (Exception e) {
LOGGER.error("打压缩包异常:{}", e);
return null;
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
LOGGER.error("打压缩包异常:{}", e);
}
}
return zipContentStream.toByteArray();
}