zoukankan      html  css  js  c++  java
  • java 压缩文件 zip 可加密

    <dependency>
        <groupId>net.lingala.zip4j</groupId>
        <artifactId>zip4j</artifactId>
        <version>2.6.4</version>
    </dependency>
    public static void zip(File currentDir, String toFilePath, String password) throws Exception {
        // 生成的压缩文件
        ZipFile zipFile = new ZipFile(toPath);
        ZipParameters parameters = new ZipParameters();
        // 压缩方式
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        // 压缩级别
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        // 是否设置加密文件
        parameters.setEncryptFiles(true);
        // 设置加密算法
        parameters.setEncryptionMethod(EncryptionMethod.AES);
        // 设置AES加密密钥的密钥强度
        parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
        // 设置密码
        if(!Strings.isNullOrEmpty(password)) {
            zipFile.setPassword(password.toCharArray());
        }
    
        // 要打包的文件夹
        File[] fs = currentDir.listFiles();
    
        // 遍历test文件夹下所有的文件、文件夹
        for (File f : fList) {
            if (f.isDirectory()) {
                zipFile.addFolder(f, parameters);
            } else {
                zipFile.addFile(f, parameters);
            }
        }
    }
    public static void unzip(String zipFilePath, String toPath, String password) throws Exception {
        // 生成的压缩文件
        ZipFile zipFile = new ZipFile(zipFilePath);
        // 设置密码
        if(!Strings.isNullOrEmpty(password)) {
            zipFile.setPassword(password.toCharArray());
        }
        // 解压缩所有文件以及文件夹
        zipFile.extractAll(toPath);
    }
  • 相关阅读:
    006_tcpdump专题
    002_阿里监控平台的“打怪升级”之路
    001_谈阿里核心业务监控平台SunFire的技术架构
    001_前端面试集合
    007_zkCli.sh
    007_Chrome的Waterfall详解
    021_supervise进行管理利器
    028_shell脚本递归求值
    MySQL -- 在磁盘爆满后复制的状态
    MySQL -- 内存使用监控详解
  • 原文地址:https://www.cnblogs.com/bevis-byf/p/14246767.html
Copyright © 2011-2022 走看看