java实现文件压缩并加密-基于zip4j

近期由于工作需要,基于zip4j实现了一个针对文件压缩并加密的功能,代码参考如下:

/**
* 
* @Title: zipFilesAndEncrypt
* @Description: 将指定路径下的文件压缩至指定zip文件,并以指定密码加密 若密码为空,则不进行加密保护
* @param srcFileName 待压缩文件路径
* @param zipFileName zip文件名
* @param password 加密密码 
* @return
* @throws Exception 
*/
public static void zipFilesAndEncrypt(String srcFileName,String zipFileName,String password) throws Exception{
ZipOutputStream outputStream = null;
InputStream inputStream = null;
if(StringUtils.isEmpty(srcFileName) || StringUtils.isEmpty(zipFileName)){
throw new NullArgumentException("待压缩文件或者压缩文件名");
}
try {
File srcFile = new File(srcFileName);   
File[] files = new File[0];
if (srcFile.isDirectory()) {
files = srcFile.listFiles();
} else {
files = new File[1];
files[0] = srcFile;
}
outputStream = new ZipOutputStream(new FileOutputStream(new File(zipFileName)));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
if(!StringUtils.isEmpty(password)){
parameters.setEncryptFiles(true);
//Zip4j supports AES or Standard Zip Encryption (also called ZipCrypto)
//If you choose to use Standard Zip Encryption, please have a look at example
//as some additional steps need to be done while using ZipOutputStreams with
//Standard Zip Encryption
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(password);
}

//Now we loop through each file and read this file with an inputstream
//and write it to the ZipOutputStream.
int fileNums = files.length;
for (int i = 0; i < fileNums; i++) {
File file = (File)files[i];

//This will initiate ZipOutputStream to include the file
//with the input parameters
outputStream.putNextEntry(file,parameters);

//If this file is a directory, then no further processing is required
//and we close the entry (Please note that we do not close the outputstream yet)
if (file.isDirectory()) {
outputStream.closeEntry();
continue;
}

inputStream = new FileInputStream(file);
byte[] readBuff = new byte[4096];
int readLen = -1;
//Read the file content and write it to the OutputStream
while ((readLen = inputStream.read(readBuff)) != -1) {
outputStream.write(readBuff, 0, readLen);
}
//Once the content of the file is copied, this entry to the zip file
//needs to be closed. ZipOutputStream updates necessary header information
//for this file in this step
outputStream.closeEntry();
inputStream.close();
}
//ZipOutputStream now writes zip header information to the zip file
outputStream.finish();
} catch (Exception e) {
log.error("文件压缩出错", e);
throw e;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error("压缩文件输出错误", e);
throw e;
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error("压缩文件错误", e);
throw e;
}
}
}
}

压缩文件方法 该方法需要引用zip4jjar文件 单个文件、多个文件压缩 /** * 使用给定密码压缩指定文件文件夹到指定位置. * * dest可传最终压缩文件存放的绝对路径,也可以传存放目录,也可以传null或者"". * 如果传null或者""则将压缩文件存放在当前目录,即跟源文件同目录,压缩文件名取源文件名,以.zip为后缀; * 如果以路径分隔符(File.separator)结尾,则视为目录,压缩文件名取源文件名,以.zip为后缀,否则视为文件名. * @param src 要压缩文件文件夹路径 * @param dest 压缩文件存放路径 * @param isCreateDir 是否在压缩文件里创建目录,仅在压缩文件为目录时有效. * 如果为false,将直接压缩目录下文件压缩文件. * @param passwd 压缩使用的密码 * @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败. */ 方法详细见文件! 可选择文件list压缩 /** * 使用给定密码压缩指定文件list * dest可传最终压缩文件存放的绝对路径,也可以传存放目录,也可以传null或者"". * 如果传null或者""则将压缩文件存放在当前目录,即跟源文件同目录,压缩文件名取源文件名,以.zip为后缀; * 如果以路径分隔符(File.separator)结尾,则视为目录,压缩文件名取源文件名,以.zip为后缀,否则视为文件名. * @param src 要压缩文件集合 * @param dest 压缩文件存放路径 * @param isCreateDir 是否在压缩文件里创建目录,仅在压缩文件为目录时有效. * 如果为false,将直接压缩目录下文件压缩文件. * @param passwd 压缩使用的密码 * @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败. */ 方法详细见文件! 解压 /** * 使用给定密码解压指定的ZIP压缩文件到指定目录 * * 如果指定目录不存在,可以自动创建,不合法的路径将导致异常被抛出 * @param zipFile 指定的ZIP压缩文件 * @param dest 解压目录 * @param passwd ZIP文件的密码 * @return 解压后文件数组 * @throws ZipException 压缩文件有损坏或者解压缩失败抛出 */ 方法详细见文件! 一个简单的demo 欢迎大家指点,一起提升
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值