java优雅的递归压缩目录
2022-12-31 本文已影响0人
不知不怪
/**
* 压缩目录
*/
public static void createZip(final String dir, final String zipFile) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
zos.putNextEntry(new ZipEntry(path.toString()));
//zos.putNextEntry(new ZipEntry(path.toString().replace("d:\\", "")));
zos.write(Files.readAllBytes(path));
return FileVisitResult.CONTINUE;
}
});
zos.close();
}