将目标文件列表存档到ZIP存档中
/**N o w J a v a . c o m - 时 代 Java**/ //package com.nowjava; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Path; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { protected static final int bufferSize = 1024; /** * Archives a list of target files into a ZIP archive * * @param archive Path to the archive * @param targetFiles List of files to put into the archive * @throws Exception */ public static void archive(Path archive, List<Path> targetFiles) throws Exception { // creates the archive byte[] buffer = new byte[bufferSize]; try (ZipOutputStream zos = new ZipOutputStream( new FileOutputStream(archive.toFile()))) { /* 时 代 J a v a 公 众 号 - nowjava.com */ // Loop through each file for (Path file : targetFiles) { ZipEntry ze = new ZipEntry(file.getFileName().toString()); zos.putNextEntry(ze); try (FileInputStream in = new FileInputStream(file.toFile())) { int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } } catch (IOException ioe_inner) { throw new Exception(