直接上代码!!复制导入包之后直接可当工具类使用!!
public class ZipUtil {
private static final int BUFFER_SIZE = 2 * 1024;
public static void fileListToZip(List<File> srcFiles , OutputStream out) throws IOException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
// 防止文件重名导致压缩失败
String fileName = srcFile.getName();
String prefix = fileName.substring(fileName.lastIndexOf("."));
String newFileName = fileName.substring(0, fileName.length()-prefix.length()) + "_" + UUIDUtil.getUUID() + prefix;
zos.putNextEntry(new ZipEntry(newFileName));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(zos != null){
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:传入的文件列表如果有重名的文件会导致压缩失败,所以要对文件名进行uuid重命名处理