使用Java8压缩文件
Java 大约 1223 字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<Path> 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");
}
}
完整代码
https://github.com/fendoudebb/learning/tree/master/java/learn-java8/zip/src
阅读 506 · 发布于 2020-05-28
————        END        ————
扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Android6.0及以上设备查看流量使用情况阅读 1906
-
MySQL Windows下cmd显示中文乱码阅读 486
-
MySQL之where和having的区别阅读 607
-
Linux 不排序去除重复行和不排序统计重复行阅读 1703
-
Golang操作MySQL数据库阅读 333
-
Spring Boot logback springProperty设置默认值阅读 332
-
数据结构:栈-链表实现阅读 77
-
MongoDB提示None of the hosts for replica set could be contacted阅读 394
-
MySQL update limit 更新限定条数阅读 258
-
前端Chrome反调试方法阅读 721