static void zipFiles(File zipfile, List<File> srcFiles) {
byte[] buf = new byte[L3vpnExportConstant.NUM_1024];
InputStream in = null;
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(Paths.get(zipfile.getCanonicalPath())))) {
// Compress the files
for (File file : srcFiles) {
in = Files.newInputStream(Paths.get(file.getCanonicalPath()));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(file.getName()));
// Transfer bytes from the file to the ZIP file
int len = in.read(buf);
while (len > 0) {
out.write(buf, 0, len);
len = in.read(buf);
}
// Complete the entry
out.closeEntry();
in.close();
}
} catch (IOException e) {
LOGGER.error("Transfer bytes from the file to the ZIP file failed. {}", e.getMessage());
} finally {
closeInputStream(in);
}
}
private static void closeInputStream(InputStream in) {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
02-20
4341
04-04
3万+
04-12
1万+