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 ...
随机推荐
- C# 3D效果饼状图的绘制
第一步:放置图表控件 拖动工具栏的Chart控件到界面上 然后选中控件,设置Series属性,将ChartType属性改为pie 第三步:设置3D效果 选择控件,设置ChartArea属性,如下 设置 ...
- yii开发一个web程序的基本流程
1. 创建目录结构.在前面的章节Creating First Yii Application写的yiic工具可以帮助我们快速完成这步. 2. 配置 application.就是修改applicatio ...
- Qt5.5中,使MainWindow初始为全屏
MainWindow w; w.showMaximized(); 实例化后,初始显示设置为最大格式即可!
- lumia520刷机注意事项
1.下载后的固件名称中设备名前的随机字符都要去掉 2.安装完nokia care suit后最好在driver目录下重新双击安装usb driver
- 使用FlaycoBanner实现图片轮播效果(加载网络图片)
FlaycoBanner是一个开源图片轮播框架,支持android2.2及以上: git地址:https://github.com/H07000223/FlycoBanner_Master 在andr ...
- jquery jqPlot API 中文使用教程
jqPlot是一个灰常强大的图表工具,曲线,柱状,饼图,应该有尽有,更要命的是,调用方便~~ 官网:http://www.jqplot.com/ 这里贡献上中文教程,基本上所有的api都很齐全,供有需 ...
- (转)JS产生随机数的几个用法!
原文 1 <script> 2 function GetRandomNum(Min,Max) 3 { 4 var Range = Max - Min; 5 var Rand = Math. ...
- 文本深度表示模型Word2Vec
简介 Word2vec 是 Google 在 2013 年年中开源的一款将词表征为实数值向量的高效工具, 其利用深度学习的思想,可以通过训练,把对文本内容的处理简化为 K 维向量空间中的向量运算,而向 ...
- 修改hive分区表,在分区列前增加一个字段
本文主要为了测试,在有数据的分区表中增加新的一个非分区字段后,新数据加入表中是否正常. 原始数据 1;zhangsan 2;zhangsan 3;zhangsan 4;lisi 5;lisi 6;li ...
- NodeOS操作系统
导读 我想大多数人听说过 Node.js,但是你听说过 NodeOS 吗?一个用 Node.js 写的操作系统,NodeOS 用 Linux 内核来处理各种底层任务,比如硬件通讯什么的,但是除此之外, ...