当前位置:   article > 正文

springBoot 文件压缩加密_springboot 压缩文件加密

springboot 压缩文件加密

1、加入依赖

		<dependency>
			<groupId>net.lingala.zip4j</groupId>
			<artifactId>zip4j</artifactId>
			<version>1.3.1</version>
		</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
 /**
     * 压缩单个文件并加密
     */
    private static String zipFile(String file, String fileOutPath, String passWord, String fileName) throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream(file);
        ZipParameters parameters = new ZipParameters();
        // 压缩方式
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        // 压缩级别
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        // 开启加密
        parameters.setSourceExternalStream(true);
        // 文件名称
        parameters.setFileNameInZip(fileName + ".xlsx");
        if (!"".equals(passWord)) {
            parameters.setEncryptFiles(true);
            // 加密方式
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
            // 设置加密密码
            parameters.setPassword(passWord.toCharArray());
        }
        try {
            ZipFile zipFile = new ZipFile(fileOutPath + fileName + ".zip");
            zipFile.addStream(fileInputStream, parameters);
            // 加密解压后删除excel
            deleteFile(file);
        } catch (ZipException e) {
            e.printStackTrace();
        }
        return fileOutPath + fileName + ".zip";
    }

	/**
     * 删除文件
     */
    private static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/984621
推荐阅读
相关标签
  

闽ICP备14008679号