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 ...
随机推荐
- GCHandler的使用
众所周知,我们在使用c#托管代码时,内存地址和GC回收那不是我们关心的,CLR已经给我们暗箱操作. 但是如果我们在c#中调用了一个非托管代码,比如vc的DLL,而且他有个回调函数,需要引用c#中的某个 ...
- Spring学习笔记(1)——资源加载
<!-- 占坑,迟点补充底层原理 --> Spring支持4种资源的地址前缀 (1)从类路径中加载资源——classpath: classpath:和classpath:/是等价的,都是相 ...
- SSH中,使用Filter拦截直接访问JSP页面!
话不多说,直接上代码 创建一个Filter类 package com.weibo.util; import java.io.IOException; import javax.servlet.Filt ...
- 【分析】Parcelable的作用
一.介绍 1.Parcelable是一个接口,可以实现序列化. 2.序列化的作用体现在:可以使用Intent来传递数据,也可以在进程建传递数据(IPC). 3.Parcelable在使用的时候,有一个 ...
- 解决 emulator-5554 disconnected! Cancelling
在命令行状态下: adb kill-server ---再adb start-server:
- JAVA 布尔型的应用
定义一个布尔类型的flag, 只是当做一个开关的意思.先定义一个标记,然后根据一些条件给这个flag赋不同的值,最后,再根据这个flag不同的值,做不同的处理. public static voi ...
- tableView的高度问题
新建tableView 到一个普通的视图控制器的View 下,如果大小是全屏高 ,你的数据最下面显示不全,需要在设置高度时候,用屏幕高度-65 即可
- JMS总结
一 什么是JMS 1.JMS,Java Message Service,Java消息服务是一种可以实现异步通讯的消息中间件MOM(Message Oriented Middleware,面向消息的中间 ...
- C# foreach,等量代换,冒泡排序
foreach: foreach (int h in a) //可以将数组读出来(自动遍历数组) { Console.WriteLi ...
- nexus私服update repair index索引失败解决方案(转)
转载地址:http://blog.csdn.net/first_sight/article/details/51559086 问题描述: 搭建Maven的Nexus私服仓库,一般安装完Nexus后,默 ...