Java压缩多个文件为Zip文件
/**
@param files 需要被压缩的文件
@param zipFile 压缩后的目标zip文件
*/
public static void zip(Path[] files, Path zipFile) throws IOException {
OutputStream outputStream = Files.newOutputStream(zipFile, StandardOpenOption.CREATE_NEW);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
for (Path file : files) {
InputStream inputStream = Files.newInputStream(file);
zipOutputStream.putNextEntry(new ZipEntry(file.getFileName().toString()));
int len;
byte[] buffer = new byte[1024 * 10];
while ((len = inputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
inputStream.close();
}
zipOutputStream.closeEntry();
zipOutputStream.close();
outputStream.close();
}
1 Like