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 ...
随机推荐
- bootstrap的下载
http://files.cnblogs.com/files/eeroom/bootstrap3.3.zip http://files.cnblogs.com/files/eeroom/Bootstr ...
- Vector和ArrayList的比较
今天研究了一下Vector和ArrayList的源码,又加深了对这两个类的理解. List接口下一共实现了三个类:ArrayList,Vector,LinkedList.LinkedList就不多说了 ...
- jdk 编译器 对final字段的处理
class FinalTest{ void a(){ final int i=10; int j=10; } } stack=2, ...
- HTML 样式属性
@charset "utf-8"; /* CSS Document */ <style> p{ /*背景与前景*/ background-color:#000;/*背景 ...
- 【汇总】Android 常用方法整理
1.解决ActionBar OverFlow按钮不显示.(在oncreate中调用即可) private void setOverflowShowingAlways() { try { ViewCon ...
- Centos7搭建java+mysql环境
前几天买了个国外的vps,打算用来练练手,准备安装mysql+jdk+tomcat+git,然后就从网上找些资料开始安装. 1.准备工具 首先,需要连接到centos,这里我用的连接工具是xshell ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作(二)
1.获取程序的Home目录 NSString *homeDirectory = NSHomeDirectory(); NSLog(@"path:%@", homeDirectory ...
- 关于MySQL5.6.25在Win7 64bit下重装后无法启动的解决方法
在重装MySQL5.6.25安装到进行配置的时候,一直在等待服务的启动.如果手动在系统服务启动会提示1067错误,这个错误在网上很常见,然而我试过了很多方法均无法解决. 于是看ProgramData\ ...
- import pysam 出错解决办法
安装pysam后,import之,结果,出现报错: Library not loaded: libcurl.4.dylib 尝试很多办法,最终发现应当这样解决: # 首先重装curl brew ins ...
- 【php学习】PHP 入门经典第一章笔记
第一章: php在线手册:http://php.net/manual/zh/index.php 在开始学习PHP之前,先来看一个合格的PHP程序员今后应具备哪些知识,这里只是笔者的一些总结,希望对读者 ...