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文件压缩与解压缩学习的更多相关文章

  1. zip4j实现文件压缩与解压缩 & common-compress压缩与解压缩

    有时候需要批量下载文件,所以需要在后台将多个文件压缩之后进行下载. zip4j可以进行目录压缩与文件压缩,同时可以加密压缩. common-compress只压缩文件,没有找到压缩目录的API. 1. ...

  2. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

  3. Huffman的应用之文件压缩与解压缩

    文件压缩与解压缩>      近期这段时间一直在学习树的这样的数据结构,也接触到了Huffman树以及了解了什仫是Huffman编码,而我们经常使用的zip压缩也是利用的Huffman编码的特性 ...

  4. Linux文件压缩、解压缩及归档工具一

    主题Linux文件压缩.解压缩及归档工具 压缩工具很重要的,因为要经常到互联网下载包 一compress/uncompress compress [-dfvcVr] [-b maxbits] [fil ...

  5. Android zip文件压缩解压缩

    DirTraversal.java <P style="TEXT-ALIGN: left; PADDING-BOTTOM: 0px; WIDOWS: 2; TEXT-TRANSFORM ...

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

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

  7. C#执行zip文件压缩的几种方法及我遇到的坑总结

    工作项目中需要用到zip压缩解压缩文件,一开始看上了Ionic.Zip.dll这个类库,操作方便,写法简单 对应有个ziphelper类 using Ionic.Zip; public static ...

  8. 使用commons-compress操作zip文件(压缩和解压缩)

    http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...

  9. 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩

    使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...

随机推荐

  1. Spark源码学习1.6——Executor.scala

    Executor.scala 一.Executor类 首先判断本地性,获取slaves的host name(不是IP或者host: port),匹配运行环境为集群或者本地.如果不是本地执行,需要启动一 ...

  2. C#同一项目中不同文件或类中的方法进行调用

    有两种方法,一是将被调用的类设置成静态类Static,这样就可以直接点出来了,二是将被调用的方法所在类设置成public,这几必须在调用类中先将被调用的类进行实体化,new()出来,再点出来. 一. ...

  3. ASP.NET 缓存技术分析

    缓存功能是大型网站设计一个很重要的部分.由数据库驱动的Web应用程序,如果需要改善其性能,最好的方法是使用缓存功能.可能的情况下尽量使用缓存,从内存中返回数据的速度始终比去数据库查的速度快,因而可以大 ...

  4. 2016 - 2 - 19 ARC内存管理知识总结(一,arc基本概念及alloc等方法的实现)

    一. ARC的基本概念 1. 在objc中采用automatic reference counting 机制, 让编译器来进行内存管理.在降低程序崩溃,内存管理泄漏等风险的同时,很大程度减少了程序员的 ...

  5. Java的流程控制和C++的异同

    Java的流程控制和C++基本相似 现将不同的地方总结一下,以便快速掌握. Java的特殊流程控制的特殊部分: 1.顺序结构  -- 没有区别 2.分之结构  -- 没有区别 3.循环结构 1> ...

  6. WPF程序如何自定义启动窗口并传参

    首先,找到App.xaml,将Application标签中的StartupUri属性去掉; 然后可以在MainWindow.xaml.cs中重载构造函数,并添加自己想要的参数; 然后在App.xaml ...

  7. Flash动画

    Flash (交互式矢量图和Web动画标准) Flash是由macromedia公司推出的交互式矢量图和 Web 动画的标准,由Adobe公 司收购.做Flash动画的人被称之为闪客.网页设计者使用 ...

  8. Dojo

    dojo的类机制支持类声明.继承.调用父类方法等功能.dojo在底层实现上是通过操作原型链来实现其类机制的,而在实现继承时采用类式继承的方式.值得一提的是,dojo的类机制允许进行多重继承(注意,只有 ...

  9. ajax登陆提示

    <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...

  10. REDIS 事务机制

    基本事务操作: 任何数据库都必须要保证一种原子执行操作:最基本的原子执行操作肯定是需要提供: 举一个例子来说明: 当对某个Key 做一个统计: 可能不同的Client做它那部分的统计,一段时间后,服务 ...