Java8
public class ZipFile {
public static Path zip(String dirPath, String zipFilePath) throws IOException {
Path zipFile = Files.createFile(Paths.get(zipFilePath));
Path sourceDirPath = Paths.get(dirPath);
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFile));
Stream paths = Files.walk(sourceDirPath)) {
paths.filter(path -> !Files.isDirectory(path))
.forEach(path -> {
ZipEntry zipEntry = new ZipEntry(sourceDirPath.relativize(path).toString());
try {
zipOutputStream.putNextEntry(zipEntry);
Files.copy(path, zipOutputStream);
zipOutputStream.closeEntry();
} catch (IOException ignore) {
}
});
}
System.out.println("Zip is created at : " + zipFile);
return zipFile;
}
public static void main(String[] args) throws IOException {
zip("D:\\dir", "D:\\test.zip");
}
}
完整代码