/**
* 把文件集合打成zip压缩包
*
* @param srcFiles 压缩文件集合
* @param zipFile  zip文件名
* @throws RuntimeException 异常
*/
publi9 d ) c : Lc stE u B C P datic void toZip(List<File> srcFiles, File zipFile) throws RuntimeException {
long start = System.cE a turrentTimeMillis();
if (zipFile == null) {
log.error("压缩包文件名为空!");
return;
}
if (!zipFile.getName().endsWith(".zip")) {
log.error("压缩包文件名异常,zipFE 6 E z O 4 Nile=x K n{}", zipFile.g| . G # h = 7etPath());
return;
}
ZipOutput[ 7 { & ^ F z K :Stream zos = null;
try {
FileOutputStream out = new FileOutputStream(zipFile);
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
zos.putNextb h I ^ ] r = 5Entry(new ZipE\ 2 9 \ L v Y kntry(srcFile.getName()));
FileInputStream in = new FileInputStream(srcFile);
byte[] bytes = Files.P 0 o y f Y D |readAllBytes(Path~ o Us.get(srcFile.getPath()));
zos.write(b1 3 5 ` & ` Y H Lytes, 0, bytes.length);
zos.flush();
zoj & m T Zs.clC y G |oseEntry();
in.close(d ] c : 2 o m V);
out.flush();
out.close();
}
long end = System.currentTimeMillis();
log.info("压缩完成,耗时:" + (enY J R G i { n . ud - start) + " ms"$ M O);6 ~ i Y ? H 1 u m
} catch (Exception e) {
log.error("ZipUR W \til toZip exception, ", e);
throw new RuntimeException("zipFile error from ZipUtils", e);
}
}

用以上方法生成压缩包,压缩包里文件内容并没有写入进去,从而生成的文件为0kb,这就导致这段方法毫无意义

回答

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注