java文件夹压缩加密

编程入门 行业动态 更新时间:2024-10-03 17:11:36

文件夹压缩加密

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.File;

/**

  • 文件夹压缩加密
    */
    public class ZipEncrypt {

    /**

    • zip加密

    • @param filePath 文件夹路径

    • @param savePath 压缩后的路径

    • @param password 密码
      */
      public static ZipFile zipEncrypt(String filePath, String savePath, String password) throws ZipException {

      File file = new File(filePath);
      ZipFile zipFile = new ZipFile(savePath);
      // 设置zip包的一些参数集合
      ZipParameters parameters = new ZipParameters();
      // 是否设置密码(此处设置为:是)
      parameters.setEncryptFiles(true);
      // 压缩方式(默认值)
      parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
      // 普通级别(参数很多)
      parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
      // 加密级别
      parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
      // 压缩包密码
      parameters.setPassword(password);

      if (file.isDirectory()) {
      zipFile.createZipFileFromFolder(file, parameters, false, -1L);
      } else {
      zipFile.createZipFile(file, parameters);
      }
      return zipFile;
      }
      }

更多推荐

java文件夹压缩加密

本文发布于:2023-06-14 01:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!