java zip4j压缩打包zip

上一个不错的,打包zip

https://github.com/srikanth-lingala/zip4j

 

下面是测试代码:

private static void zipFile() throws ZipException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println(sdf.format(new Date()));
        // 生成的压缩文件
        ZipFile zipFile = new ZipFile("D:\\test.zip");
        ZipParameters parameters = new ZipParameters();
        // 压缩方式
        parameters.setCompressionMethod(CompressionMethod.STORE);
        // 压缩级别
        parameters.setCompressionLevel(CompressionLevel.FASTEST);
        // 要打包的文件夹
        File currentFile = new File("D:\\abc");
        File[] fs = currentFile.listFiles();
        // 遍历test文件夹下所有的文件、文件夹
        for (File f : fs) {
            if (f.isDirectory()) {
                zipFile.addFolder(f, parameters);
            } else {
                zipFile.addFile(f, parameters);
            }
        }
        System.out.println(sdf.format(new Date()));
    }

安装:maven

<!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j -->
<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>2.6.1</version>
</dependency>

 

posted @ 2020-08-25 14:17  jiduoduo  阅读(1466)  评论(0编辑  收藏  举报