• Zip4j使用


    引用:https://www.jianshu.com/p/89bf65317e6b

    ZIp4j :支持加密,解密压缩,支持文件的添加,删除,等

    <dependency>
        <groupId>net.lingala.zip4jgroupId>
        <artifactId>zip4jartifactId>
        <version>1.3.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public interface Zip4jConstants {
        // 默认
        Integer COMP_DEFLATE = 8;
        // 仅打包,不压缩
        Integer COMP_STORE = 0;
        // 加密压缩
        Integer COMP_AES_ENC =99;
    
    
        Integer DEFLATE_LEVEL_FASTEST =1;   // (速度最快,压缩比最小)
    
        Integer DEFLATE_LEVEL_FAST = 3; //(速度快,压缩比小)
        Integer  DEFLATE_LEVEL_NORMAL = 5; //(一般)
        Integer DEFLATE_LEVEL_MAXIMUM = 7;
        Integer  DEFLATE_LEVEL_ULTRA = 9;
    
    
    
        static final int ENC_NO_ENCRYPTION = -1;//(默认,没有加密方法,如果采用此字段,会报错”没有提供加密算法”)
        static final int ENC_METHOD_STANDARD = 0;
        static final int ENC_METHOD_AES = 99;
    
        static final int AES_STRENGTH_128 = 0x01;
        static final int AES_STRENGTH_192 = 0x02;
        static final int AES_STRENGTH_256 = 0x03;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    package com.cj.zip;
    
    import net.lingala.zip4j.core.ZipFile;
    import net.lingala.zip4j.exception.ZipException;
    import net.lingala.zip4j.model.ZipParameters;
    
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.util.ArrayList;
    
    public class Demo {
        
        public static void zipFile() throws ZipException {
            // 生成的压缩文件
            ZipFile zipFile = new ZipFile("F:\\images.zip");
            ZipParameters parameters = new ZipParameters();
            // 压缩方式
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            // 压缩级别
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    
            // 要打包的文件夹
            File currentFile = new File("F:\\images");
    
            File[] files = currentFile.listFiles();
            for (File f : files) {
                if (f.isDirectory()){
                    zipFile.addFolder(f.getPath(),parameters);
                }else{
                    zipFile.addFile(f,parameters);
                }
            }
        }
    
    
        public static void unzip() throws ZipException {
            ZipFile zipFile = new ZipFile("F:\\images.zip");
            zipFile.extractAll("f:\\aa");
    
        }
    
    
    
        public static void addFile() throws ZipException {
            ZipFile zipFile = new ZipFile("F:\\images.zip");
            ArrayList<Object> addFiles = new ArrayList<>();
            addFiles.add(new File("F:\\1.txt"));
            addFiles.add(new File("F:\\1.pdf"));
    
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            // 目标路径
            parameters.setRootFolderInZip("cc/");
    //        zipFile.addFiles(addFiles,parameters);
            zipFile.addFile(new File("F:\\1.txt"),parameters);
            zipFile.addFile(new File("F:\\1.pdf"),parameters);
        }
    
        public static  void addFile02() throws ZipException {
            ZipFile zipFile = new ZipFile("f://images.zip");
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(8);
            // 设置名字
            parameters.setFileNameInZip("cc/hehe.txt");
            parameters.setSourceExternalStream(true);
    
            ByteArrayInputStream inputStream = new ByteArrayInputStream(new String("silly b").getBytes());
            zipFile.addStream(inputStream,parameters);
        }
    
    
        public static void deleteFile() throws ZipException {
            ZipFile zipFile = new ZipFile("f://images.zip");
            // 删除压缩文件中文件
            zipFile.removeFile("cc/hehe.txt");
        }
    
        private static  void zipFileWithPwd() throws ZipException {
            ZipFile zipFile = new ZipFile("f://images1.zip");
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            // 设置加密
            parameters.setEncryptFiles(true);
            // 加密方式
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
            parameters.setPassword("123");
    
            File currentFile = new File("f://images");
            File[] files = currentFile.listFiles();
            for (File f : files) {
                if (f.isDirectory()){
                    zipFile.addFolder(f.getPath(),parameters);
                }else{
                    zipFile.addFile(f,parameters);
                }
            }
        }
    
        public static void main(String[] args) throws ZipException {
    //        zipFile();
    //        unzip();
    //        addFile02();
    //        addFile();
    //        deleteFile();
            zipFileWithPwd();
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
  • 相关阅读:
    第一章 - 第4节-计算机软件系统 - 课后习题
    SpringBoot+Mybatis-Plus+Redis+Vue在线博客系统,叮当外卖单体项目,Python爬虫可视化,面试,扫盲链接
    redis-shake同步数据
    Java实训:学生信息管理系统
    PLC可以连接哪些工业设备实现远距离无线通讯?工业网关可以吗?
    mybatis1:简介
    金融云行至“深水区”
    1318_将ST link刷成jlink
    【debian系统arm架构安装docker】且换源后依旧不行就离线导入镜像
    堆(二叉堆)-优先队列-数据结构和算法(Java)
  • 原文地址:https://blog.csdn.net/qq_36022463/article/details/126140775