加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

使用zip4j加密和解密文件和目录

发布时间:2020-12-15 03:20:59 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 package com.ilucky.zip4j.util;import java.io.File;import net.lingala.zip4j.core.ZipFile;import net.lingala.zip4j.exception.ZipException;impo

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

package com.ilucky.zip4j.util;

import java.io.File;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

/**
 * @author IluckySi
 * @since 20150723
 */
public class Zip4jUtil {

    private String srcPath;
    private String dstPath;
    private String password = "123456";

    public String getSrcPath() {
        return srcPath;
    }
    public void setSrcPath(String srcPath) {
        this.srcPath = srcPath;
    }
    public String getDstPath() {
        return dstPath;
    }
    public void setDstPath(String dstPath) {
        this.dstPath = dstPath;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 加密
     * 支持将某个文件或某个目录下所有的文件加密.
     * 1.某个文件:D:testsrc.zip.
     * 2某个目录:D:testsrc
     * @return boolean
     */
    public boolean encrypt() {
        try {
            if(!new File(srcPath).exists()) {
                System.out.println("源路径不存在 "+srcPath);
                return false;
            }
            ZipParameters parameters = new ZipParameters();  
            parameters.setEncryptFiles(true);  
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);  
            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);  
            parameters.setPassword(password.toCharArray());  
            File srcFile = new File(srcPath);
            ZipFile destFile = new ZipFile(dstPath);  
            if(srcFile.isDirectory()) {
                 destFile.addFolder(srcFile,parameters);  
            } else {
                destFile.addFile(srcFile,parameters); 
            }
            System.out.println("成功加密文件");
            return true;
        } catch (Exception e) {
            System.out.println("加密文件发生异常:"+e);
            return false;
        }
    }

    /**
     * 解密
     * 支持将某个加密文件解压缩到某个指定目录下面.
     * @return boolean
     */
    public boolean decrypt() {
         try {
             if(!new File(srcPath).exists()) {
                 System.out.println("源路径不存在 "+srcPath);
                return false;
             }
             ZipFile srcFile = new ZipFile(srcPath);  
             srcFile.setFileNameCharset("GBK");  
             srcFile.setPassword(password.toCharArray());  
             srcFile.extractAll(dstPath);
             System.out.println("成功解密文件");
             return true;
        } catch (ZipException e) {
            System.out.println("解密文件发生异常:"+e);
            return false;
        }
    }
}

然后看测试类:
package com.ilucky.zip4j.util;

/**
 * @author IluckySi
 * @since 20150723
 */
public class MainTest {

    public static void main(String[] args) { 
        //加密.
        Zip4jUtil zip4jUtil  = new Zip4jUtil();
        zip4jUtil.setSrcPath("D:testsrc.zip");
        zip4jUtil.setDstPath("D:testdst.zip");
        zip4jUtil.setPassword("123");
        zip4jUtil.encrypt();

        //解密.
        zip4jUtil.setSrcPath("D:testdst.zip");
        zip4jUtil.setDstPath("D:test");
        zip4jUtil.setPassword("123");
        //zip4jUtil.decrypt();
    }
}

最后看pom文件:
 <dependency>
        <groupId>net.lingala.zip4j</groupId>
        <artifactId>zip4j</artifactId>
        <version>1.3.2</version>
    </dependency>

来自:http://blog.csdn.net/sidongxue2/article/details/47026909

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读