ZIP4J---ZIP文件压缩与解压缩学习
package com.wbh.common.utils; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import org.apache.log4j.Logger; import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants; public class ZipUtils { /** log日志 */
private static final Logger logger = Logger.getLogger(ZipUtils.class);
/*
* 将一个路径下的文件打zip
*
* @folderToAdd 文件路径
*
* @targetZipFilePath zip文件存放路径
*/
public String zipForder(String folderToAdd, String targetZipFilePath) {
String result = null;
if (FileHelper.isExistsFile(targetZipFilePath) || FileHelper.isExistsFolder(folderToAdd) == false) {
return result;
}
try {
ZipFile zipFile = new ZipFile(targetZipFilePath);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.addFolder(folderToAdd, parameters);
result = targetZipFilePath;
} catch (ZipException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return result;
} /*
* 将一个路径下的文件打zip
*
* @folderToAdd 文件路径
*
* @targetZipFilePath zip文件存放路径
*
* @password 解压密码
*/
public String zipForder(String folderToAdd, String targetZipFilePath,String targetFileName, String password) {
String result = null;
try {
if (FileHelper.isExistsFile(targetZipFilePath+targetFileName) || FileHelper.isExistsFolder(folderToAdd) == false) {
return result;
}
if (FileHelper.isExistsFolder(targetZipFilePath) == false) {
FileHelper.createFolder(targetZipFilePath);
} } catch (Exception e) { } try {
ZipFile zipFile = new ZipFile(targetZipFilePath+targetFileName);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
// 加密设置
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(password);
zipFile.addFolder(folderToAdd, parameters);
result = targetZipFilePath;
} catch (ZipException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return result;
} /*
* 将一个文件流打zip
*
* @is 文件流
*
* @sourcefilePath 对应文件流的文件名(全路径)
*
* @targetZipFilePath zip文件存放路径
*
* @fileNameInZip zip文件中文件的命名
*/
public String AddStreamToZip(InputStream is, String sourcefilePath, String targetZipFilePath, String fileNameInZip) {
String result = null;
try {
ZipFile zipFile = new ZipFile(targetZipFilePath);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setFileNameInZip(fileNameInZip);
parameters.setSourceExternalStream(true); is = new FileInputStream(sourcefilePath);
zipFile.addStream(is, parameters);
result = targetZipFilePath;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
} /*
* 将多个文件打zip
*
* @filesToAdd 文件集合
*
* @targetZipFilePath zip文件存放路径
*
* @password 解压密码
*/
public String zipFiles(ArrayList<File> filesToAdd, String targetZipFilePath, String password) {
String result = null;
if (FileHelper.isExistsFile(targetZipFilePath)) {
return result;
}
for (int i = 0; i < filesToAdd.size(); i++) {
if (filesToAdd.get(i).exists() == false) {
// 有不存在的文件就退出
return result;
}
}
try {
ZipFile zipFile = new ZipFile(targetZipFilePath);
zipFile.setFileNameCharset("GBK");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(password);
zipFile.addFiles(filesToAdd, parameters);
result = targetZipFilePath;
} catch (ZipException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return result;
} /*
* 解压zip
*
* @zipFilePath zip文件存放路径
*
* @unZipFilePath 解压zip文件存放路径
*
* @password 解压密码
*/
public String ExtractAllFiles(String zipFilePath,String unZipFilePath, String password) {
String result = null;
if (FileHelper.isExistsFile(zipFilePath)) {
return result;
}
try {
ZipFile zipFile = new ZipFile(zipFilePath);
if (zipFile.isEncrypted()) {
// if yes, then set the password for the zip file
zipFile.setPassword(password);
}
zipFile.extractAll(unZipFilePath); } catch (ZipException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return result;
} }
ZIP4J---ZIP文件压缩与解压缩学习的更多相关文章
- zip4j实现文件压缩与解压缩 & common-compress压缩与解压缩
有时候需要批量下载文件,所以需要在后台将多个文件压缩之后进行下载. zip4j可以进行目录压缩与文件压缩,同时可以加密压缩. common-compress只压缩文件,没有找到压缩目录的API. 1. ...
- 利用Java进行zip文件压缩与解压缩
摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...
- Huffman的应用之文件压缩与解压缩
文件压缩与解压缩> 近期这段时间一直在学习树的这样的数据结构,也接触到了Huffman树以及了解了什仫是Huffman编码,而我们经常使用的zip压缩也是利用的Huffman编码的特性 ...
- Linux文件压缩、解压缩及归档工具一
主题Linux文件压缩.解压缩及归档工具 压缩工具很重要的,因为要经常到互联网下载包 一compress/uncompress compress [-dfvcVr] [-b maxbits] [fil ...
- Android zip文件压缩解压缩
DirTraversal.java <P style="TEXT-ALIGN: left; PADDING-BOTTOM: 0px; WIDOWS: 2; TEXT-TRANSFORM ...
- Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)
1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...
- C#执行zip文件压缩的几种方法及我遇到的坑总结
工作项目中需要用到zip压缩解压缩文件,一开始看上了Ionic.Zip.dll这个类库,操作方便,写法简单 对应有个ziphelper类 using Ionic.Zip; public static ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩
使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...
随机推荐
- mybatis学习2
解决字段名与实体类属性名不相同的冲突 1. 准备表和数据:CREATE TABLE orders(order_id INT PRIMARY KEY AUTO_INCREMENT,order_no VA ...
- K均值聚类算法的MATLAB实现
1.K-均值聚类法的概述 之前在参加数学建模的过程中用到过这种聚类方法,但是当时只是简单知道了在matlab中如何调用工具箱进行聚类,并不是特别清楚它的原理.最近因为在学模式识别,又重新接触了这 ...
- Java Swing 第01记 Hello Word
首先来一个Java Swing的HelloWord程序. package cn.java.swing.chapter03; import javax.swing.JButton; import jav ...
- javascript运动应用
多物体运动框架: 1.多个盒子同时运动:move(obj,target)多一个参数 <!DOCTYPE html><html><head> <title> ...
- C#单链表
顺序表是用地址连续的存储单元顺序存储线性表中的各个数据元素, 逻辑上相邻的数据元素在物理位置上也相邻.因此,在顺序表中查找任何一个位置上的数据元素非常方便, 这是顺序存储的优点. 但是, 在对顺序表进 ...
- 如何评价苹果中国官网 iOS 8 介绍页面的文案「开发者的大事、大快所有人心的大好事」?[转自知乎]
在什么是「苹果式中文」答案中,小七得出了这个结论: 「苹果式中文」是指句子结构破碎,经常缺乏主语,滥用排比,顶真,偏正短语,和不恰当四字词的广告文体. (有关什么是苹果式中文,小七原来贴错地方了TAT ...
- hash连接
简单回顾嵌套循环: 两个表关联,较小的表(指使用了过滤条件后结果集较小的表)称为驱动表或者外表(,另一个称为内表.在嵌套连接过程中,oracle首先读取驱动表的第一条数据,然后和内表进行比对,所以匹配 ...
- C++ 容器 LIST VECTOR erase
在Vector中 做erase操作就是按照下面的步骤来做的: copy() destory(); 在list容器中 erase操作 destory() deallocate() Vector使用从某 ...
- K - Least Common Multiple
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
- 利用 iframe解决ajax的跨域问题
问题 1. form提交或a标签跳转方式提交不会引发跨域问题. 2. ajax出于安全问题就有了跨域问题,因为一次请求中既访问了外部域最后返回了自己的域. 3. 用iframe其实就是想仿照ajax的 ...