美文网首页
java优雅的递归压缩目录

java优雅的递归压缩目录

作者: 不知不怪 | 来源:发表于2022-12-31 10:54 被阅读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();
    }

相关文章

网友评论

      本文标题:java优雅的递归压缩目录

      本文链接:https://www.haomeiwen.com/subject/yszfcdtx.html