今天的新需求,希望上传zip包之后进行加密。

虽然java.util里有zip工具类,但是有种种限制,比如不支持中文文件名之类的。于是在网上找轮子,看到 zip4j 感觉不错。

开搞

添加Maven依赖

<dependency><groupId>net.lingala.zip4j</groupId><artifactId>zip4j</artifactId><version>1.3.2</version>
</dependency>

为了方便入门,从官网下载了示例

官网:http://www.lingala.net/zip4j/

示例:http://www.lingala.net/zip4j/download.php

示例代码:

基本的使用情景都有了。

api的基本用法大致如下:

1.创建ZipFile对象

2.创建ZipParameters对象,并使用该对象配置属性

3.ZipFIle对象有addFile/addFiles等方法,在此传入File/InputStream/目录 + 配置好的ZipParameters对象即可压缩。

需要加密时在ZipParameters对象中配置对应的参数即可。

看一下加密的例子:

普通加密:

// Set the encryption flag to true
// If this is set to false, then the rest of encryption properties are ignored
parameters.setEncryptFiles(true);// Set the encryption method to Standard Zip Encryption
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);// Set password
parameters.setPassword("test123!");

AES加密:

// Set the encryption flag to true
// If this is set to false, then the rest of encryption properties are ignored
parameters.setEncryptFiles(true);// Set the encryption method to AES Zip Encryption
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);// Set AES Key strength. Key strengths available for AES encryption are:
// AES_STRENGTH_128 - For both encryption and decryption
// AES_STRENGTH_192 - For decryption only
// AES_STRENGTH_256 - For both encryption and decryption
// Key strength 192 cannot be used for encryption. But if a zip file already has a
// file encrypted with key strength of 192, then Zip4j can decrypt this file
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);// Set password
parameters.setPassword("test123!");

看过例子试着写了第一版代码熟悉熟悉:

private Map zipFileWithPass(CommonsMultipartFile file,String filePath,String password) {Map resultMap = new HashMap();try {log.debug("开始zip加密");//创建zip文件ZipFile zipFile = new ZipFile(filePath);//准备压缩参数ZipParameters parameters = new ZipParameters();//压缩算法parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);//压缩等级parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setIncludeRootFolder(false);//是否加密parameters.setEncryptFiles(true);
//            //使用AES加密
//            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
//            //AES256
//            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);//使用标准加密方式parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);//设置加密密钥parameters.setPassword(password);//CommonsMultipartFile 转  FileDiskFileItem diskFileItem = (DiskFileItem)file.getFileItem();log.debug("diskFileItem.getName()" + diskFileItem.getName());File normalFile = diskFileItem.getStoreLocation();log.debug("normalFile.getName()" + normalFile.getName());//添加到压缩文档zipFile.addFile(normalFile, parameters);
//            zipFile.addStream(in, parameters);resultMap.put("error", 0);}catch (Exception e) {// TODO: handle exceptione.printStackTrace();log.debug(e.toString());resultMap.put("error", 1);resultMap.put("msg", e.getMessage());}finally {return resultMap;}
}

这样初步实现了加密压缩,但是需求中上传的文件已经是zip了,此时的结果是zip包zip。

于是决定改变思路,将文件解压再重压缩:(应该有更好的方法)

private Map unZipFileToFolder(CommonsMultipartFile file, String extractPath) {Map resultMap = new HashMap();try {log.debug("将文件解压到" + extractPath);DiskFileItem diskFileItem = (DiskFileItem)file.getFileItem();log.debug("diskFileItem.getName()" + diskFileItem.getName());File normalFile = diskFileItem.getStoreLocation();log.debug("normalFile.getName()" + normalFile.getName());ZipFile zipFile = new ZipFile(normalFile);zipFile.extractAll(extractPath);log.debug("将文件解压到" + extractPath + " 成功");}catch (Exception e) {// TODO: handle exceptione.printStackTrace();log.debug("将文件解压到" + extractPath + " 失败");}finally {return resultMap;}
}
private Map zipFolderToZip(String extractPath, String filePath, String password) {Map resultMap = new HashMap();try {log.debug("将" + extractPath + "目录下的文件打包并加密");// Initiate ZipFile object with the path/name of the zip file.ZipFile zipFile = new ZipFile(filePath);// Initiate Zip Parameters which define various properties such// as compression method, etc.ZipParameters parameters = new ZipParameters();// set compression method to store compressionparameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);// Set the compression levelparameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);//是否加密parameters.setEncryptFiles(true);
//            //使用AES加密
//            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
//            //AES256
//            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);//使用标准加密方式parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);//设置加密密钥parameters.setPassword(password);parameters.setIncludeRootFolder(false);// Add folder to the zip filezipFile.addFolder(extractPath, parameters);//         File file = new File(filepath);//File类型可以是文件也可以是文件夹
//          File[] fileArray = file.listFiles();//         ArrayList<File> fileList = getAllFilesFromPath(filepath);
//          ArrayList<File> fileList = Zip4jUtil.getFilesInDirectoryRec(new File(extractPath), false);//         for(File subfile: fileArray) {
//              fileList.add(subfile);
//          }
//          zipFile.addFiles(fileList, parameters);log.debug("将" + extractPath + "目录下的文件打包并加密 成功");} catch (ZipException e) {e.printStackTrace();log.debug("将" + extractPath + "目录下的文件打包并加密 失败");}finally {return resultMap;}
}

注意在此压缩时加上

parameters.setIncludeRootFolder(false);

否则zip包内会多套一层目录

【日记】 使用 zip4j 实现压缩包加密相关推荐

  1. zip4j对处理压缩包及压缩包加密处理

    zip4j对处理压缩包及压缩包加密处理,代码如下: package com.zip4j;import java.io.File; import java.util.ArrayList; import ...

  2. Zip4j 压缩包加密压缩与解压

    引入依赖:zip4j <dependency><groupId>net.lingala.zip4j</groupId><artifactId>zip4j ...

  3. java压缩包加密上传,解密下载

    java压缩包加密上传,解密下载 业务场景 供应商上传投标文件需要进行加密处理,通过链接下载时下载的时加密的文件,而通过系统内访问接口下载时,下载的是解密好的文件. 问题解决思路 1. 首先配置三个保 ...

  4. 压缩包加密后门_加密后门:还有更多争论吗?

    压缩包加密后门 "我认为,要求制造硬件和软件的公司构建重复的密钥或后门是错误的,即使您对将要法院下达命令的观念进行套期保值.我也说了很多原因,我已经考虑了很多." 随着加密访问辩论 ...

  5. 【加解密篇】利用HashCat破解RAR压缩包加密文件详细教程

    [加解密篇]利用HashCat解密RAR压缩包加密文件 在取证知识里挖呀挖呀挖-[蘇小沐] 文章目录 [加解密篇]利用HashCat解密RAR压缩包加密文件 1.实验环境 2.RAR加密压缩包 (一) ...

  6. ZIP/RAR压缩包加密原理和解密方法

    ZIP/RAR压缩包加密原理和解密方法 1.压缩包的概念 一般我们看到的压缩格式有.rar,.zip,等等有许多格式但主要压缩的作用就是让某一个文件占用空间小点.比如原来是50MB,可以压缩到30多M ...

  7. 压缩包加密后门_什么是加密后门?

    压缩包加密后门 deepadesigns/Shutterstockdeepadesigns /快门 You might have heard the term "encryption bac ...

  8. ZIP压缩包加密、解密

    ZIP压缩包文件可以设置加密来保护文件内容,今天和大家分享如何对压缩包文件进行加密.以及如何删除压缩包密码.还有忘记了压缩包密码该如何解决问题. 压缩包加密 压缩包加密是在压缩文件的时候进行的,我们右 ...

  9. 2021-11-30 Misc 杂项略览 二 压缩包加密【T.O.CTF】

    大家有遇到过压缩包加密的苦恼吗?我遇到过,在下载好了心心念念的 新世纪福音战士新剧场版:||之后,准备解压大快朵颐,解压软件却提示要你输入密码,而你恰恰又不知道,我相信你的心态一定会爆炸.这类让人心烦 ...

最新文章

  1. C/C++/Java 的基本数据类型
  2. 机器学习--多元线性回归
  3. ABAP 标准培训教程 BC400 学习笔记之一:ABAP 服务器的架构和一个典型的 ABAP 程序结构介绍
  4. python request下载文件_Python3.4.3使用urllib.request下载文件带进度显示
  5. 一个小栗子聊聊JAVA泛型基础
  6. 分数诚可贵的飞鸽传书2012绿色版
  7. 让我们来比较C#,C++和Java之间重写虚函数的区别
  8. java list 树_java list转换为树形
  9. 12. Integer to Roman
  10. Alamofire拦截请求AOP,URLProtocol
  11. cruzer php sandisk 闪迪u盘量产工具_闪迪sandisk U盘不量产修复方法教程
  12. 球面坐标系与三角函数 Spherical Coordinates and Trigonometric Functions
  13. 取文件操作fopen(file open)
  14. 输出任意乘法 口诀表
  15. 2022-2028全球踏步机测力计行业调研及趋势分析报告
  16. 复制 python cache 文件到预训练模型
  17. linux内核中的文件描述符(四)--fd的分配--get_unused_fd
  18. 开源FPGA开发板-OpenICE 介绍及抽奖
  19. Web 攻防之业务安全:登录失败信息测试.
  20. 雷蛇显示服务器,雷蛇(Razer)数据泄漏暴露了游戏玩家的个人信息

热门文章

  1. 程序员文档写作能力(二)-大三段式构架你的文档
  2. 1.7 使用不同设备类型的iOS模拟器 [原创iOS开发-Xcode教程]
  3. ROS学习笔记 程序简单示例一:发布者与订阅者(talker和listener)
  4. 如何使用Excel管理项目?
  5. 基于python管理系统论文_基于Python语言的实验室管理系统的设计与实现
  6. 华为OD-Java面经
  7. PDF怎么合并?教你2个免费一键合并PDF的方法
  8. Cision与Brandwatch达成收购协议,整合公关、社交媒体管理和数字消费者洞察
  9. 系统渗透与防护——网络安全
  10. 构造方法,接口有无构造方法