原文:http://blog.csdn.net/k21325/article/details/54376188

1.首先,引用到zip4j的第三方类库,感谢作者的无私奉献,官网打不开,这里就不贴了,下面是maven仓库的jar包

<!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>

2.下面是压缩和解压缩可选附加密码的工具类

import java.io.File;
import java.util.ArrayList;
import java.util.List; import org.springframework.util.StringUtils; import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants; public class ZipUtils {
/**
* 根据给定密码压缩文件(s)到指定目录
*
* @param destFileName 压缩文件存放绝对路径 e.g.:D:/upload/zip/demo.zip
* @param passwd 密码(可为空)
* @param files 单个文件或文件数组
* @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败.
*/
public static String compress(String destFileName, String passwd, File... files) {
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别
if (!StringUtils.isEmpty(passwd)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
parameters.setPassword(passwd.toCharArray());
}
try {
ZipFile zipFile = new ZipFile(destFileName);
for (File file : files) {
zipFile.addFile(file, parameters);
}
return destFileName;
} catch (ZipException e) {
e.printStackTrace();
} return null;
} /**
* 根据给定密码压缩文件(s)到指定位置
*
* @param destFileName 压缩文件存放绝对路径 e.g.:D:/upload/zip/demo.zip
* @param passwd 密码(可为空)
* @param filePaths 单个文件路径或文件路径数组
* @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败.
*/
public static String compress(String destFileName, String passwd, String... filePaths) {
int size = filePaths.length;
File[] files = new File[size];
for (int i = 0; i < size; i++) {
files[i] = new File(filePaths[i]);
}
return compress(destFileName, passwd, files);
} /**
* 根据给定密码压缩文件(s)到指定位置
*
* @param destFileName 压缩文件存放绝对路径 e.g.:D:/upload/zip/demo.zip
* @param passwd 密码(可为空)
* @param folder 文件夹路径
* @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败.
*/
public static String compressFolder(String destFileName, String passwd, String folder) {
File folderParam = new File(folder);
if (folderParam.isDirectory()) {
File[] files = folderParam.listFiles();
return compress(destFileName, passwd, files);
}
return null;
} /**
* 根据所给密码解压zip压缩包到指定目录
* <p>
* 如果指定目录不存在,可以自动创建,不合法的路径将导致异常被抛出
*
* @param zipFile zip压缩包绝对路径
* @param dest 指定解压文件夹位置
* @param passwd 密码(可为空)
* @return 解压后的文件数组
* @throws ZipException
*/
@SuppressWarnings("unchecked")
public static File[] deCompress(File zipFile, String dest, String passwd) throws ZipException {
//1.判断指定目录是否存在
File destDir = new File(dest);
if (destDir.isDirectory() && !destDir.exists()) {
destDir.mkdir();
}
//2.初始化zip工具
ZipFile zFile = new ZipFile(zipFile);
zFile.setFileNameCharset("UTF-8");
if (!zFile.isValidZipFile()) {
throw new ZipException("压缩文件不合法,可能被损坏.");
}
//3.判断是否已加密
if (zFile.isEncrypted()) {
zFile.setPassword(passwd.toCharArray());
}
//4.解压所有文件
zFile.extractAll(dest);
List<FileHeader> headerList = zFile.getFileHeaders();
List<File> extractedFileList = new ArrayList<File>();
for(FileHeader fileHeader : headerList) {
if (!fileHeader.isDirectory()) {
extractedFileList.add(new File(destDir,fileHeader.getFileName()));
}
}
File [] extractedFiles = new File[extractedFileList.size()];
extractedFileList.toArray(extractedFiles);
return extractedFiles;
}
/**
* 解压无密码的zip压缩包到指定目录
* @param zipFile zip压缩包
* @param dest 指定解压文件夹位置
* @return 解压后的文件数组
* @throws ZipException
*/
public static File[] deCompress(File zipFile, String dest){
try {
return deCompress(zipFile, dest, null);
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据所给密码解压zip压缩包到指定目录
* @param zipFilePath zip压缩包绝对路径
* @param dest 指定解压文件夹位置
* @param passwd 压缩包密码
* @return 解压后的所有文件数组
*/
public static File[] deCompress(String zipFilePath, String dest, String passwd){
try {
return deCompress(new File(zipFilePath), dest, passwd);
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
/**
* 无密码解压压缩包到指定目录
* @param zipFilePath zip压缩包绝对路径
* @param dest 指定解压文件夹位置
* @return 解压后的所有文件数组
*/
public static File[] deCompress(String zipFilePath, String dest){
try {
return deCompress(new File(zipFilePath), dest, null);
} catch (ZipException e) {
e.printStackTrace();
}
return null;
} public static void main(String[] args) {
// String folder = "D:\\upload\\backup\\down\\dezip\\";
// compress("D:/upload/zip/测试.zip", "123456", folder); deCompress("D:/upload/zip/测试.zip", "D:/upload/zip/unzip", "123456"); }
}

zip4j加密压缩、解压缩文件、文件夹的更多相关文章

  1. Zip压缩/解压缩(文件夹)

    #PS2.0压缩为.zip文件: $zip = "D:\audit_log\test.zip"New-Item $zip -ItemType file$shellApplicati ...

  2. 让Ubuntu可以压缩/解压缩RAR文件

    ubuntu刚安装的时候是不能解压rar文件的,只有在安装了解压工具之后,才可以解压. 安装:sudo apt-get install unrar卸载:sudo apt-get remove unra ...

  3. python开发_gzip_压缩|解压缩gz文件_完整版_博主推荐

    ''' gzip -- 支持gzip文件 源文件:Lib/gzip.py 这个模块提供了一些简单的接口来对文件进行压缩和解压缩,类似于GNU项目的gzip和gunzip. 数据的压缩源于zlib模块的 ...

  4. 使用VC++压缩解压缩文件夹

    前言   项目中要用到一个压缩解压缩的模块, 看了很多文章和源代码,  都不是很称心, 现在把我自己实现的代码和大家分享. 要求: 1.使用Unicode(支持中文). 2.使用源代码.(不使用静态或 ...

  5. 使用GZipStream压缩和解压文件

    最近做了一个用.NET里的GZipStream压缩解压缩gzip文件的小程序. GZipStream在System.IO.Compression底下,使用起来也很简单.虽然GZipStream是Str ...

  6. C#压缩、解压缩文件(夹)(rar、zip)

    主要是使用Rar.exe压缩解压文件(夹)(*.rar),另外还有使用SevenZipSharp.dll.zLib1.dll.7z.dll压缩解压文件(夹)(*.zip).需要注意的几点如下: 1.注 ...

  7. C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 C# 文件压缩加解密

    C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩   这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http:// ...

  8. Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)

    1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...

  9. 关于SharpZipLib压缩分散的文件及整理文件夹的方法

    今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为SharpZipLib没有提供压缩进某个指定文件夹的功能,在反复分析了SharpZipLib提供的各个接口方法后,终于找到 ...

随机推荐

  1. Farseer.net轻量级ORM开源框架 V1.x 入门篇:存储过程数据操作

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:存储过程实体类映射 下一篇:Farseer.net轻量级ORM ...

  2. Farseer.net轻量级ORM开源框架 V1.2版本升级消息

    V1.1到V1.2的更新,重构了很多类及方法,其中主要做了性能优化(取消所有反射,使用表达式树+缓存).解耦了SQL生成层(没有实体.队列的依赖,所有数据均通过表达式树传递解析) 先上内部更新历史记录 ...

  3. Farseer.net轻量级ORM开源框架 V1.x 入门篇:新版本说明

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:没有了 下一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库配置 前言 V1.x版本终于到来了.本次 ...

  4. leetcode_654. Maximum Binary Tree

    https://leetcode.com/problems/maximum-binary-tree/ 给定数组A,假设A[i]为数组最大值,创建根节点将其值赋为A[i],然后递归地用A[0,i-1]创 ...

  5. 【java】查重类的实现

    import java.util.Vector; public class ElementCheck { // 重复优先 static Vector<Integer> CheckSameE ...

  6. bind - 将一个名字和一个套接字绑定到一起

    SYNOPSIS 概述 #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct s ...

  7. spring的设计思想

    在学习Spring框架的时候, 第一件事情就是分析Spring的设计思想 在学习Spring的时候, 需要先了解耦合和解耦的概念 耦合: 简单来说, 在软件工程当中, 耦合是指对象之间的相互依赖 耦合 ...

  8. S-HR之导入模板指向实现类配置

    SELECT * FROM t_bs_basefileimpmap where FENTITYNAME='com.kingdee.eas.hr.affair.app.ResignBizBillEntr ...

  9. Linux C动态链接库实现一个插件例子

    实现一个简单的计算动态链接库:升级动态链接库后,在不重新编译主程序的情况下,直接生效. lib库: #cat math.c #include <stdio.h> int add(int x ...

  10. ArrayList中removeAll和clear的区别(无区别)

    removeAll会直接调用此方法,传入list和false,因中间的逻辑都不会走(如果由retainAll方法调用,则会走这些逻辑判断),所以只需要看finaly中的最后一个if条件,w=0,通过循 ...