java zip工具类
依赖jar :apache-ant-1.9.2-bin.zip
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List; import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert; /**
* zip工具
* 用于制作压缩包和解压包
* @author Sun Qinbo
* @version 1.0, 2013-7-26
*/
public class ZipUtils {
private static final Logger log = LoggerFactory.getLogger(ZipUtils.class);
private ZipOutputStream zipOut;
private static byte[] buf = new byte[1024]; /**
* 制作压缩包
*/
public static void zip(OutputStream out, List<FileEntry> fileEntrys, String encoding) {
new ZipUtils(out, fileEntrys, encoding);
} /**
* 制作压缩包
*/
public static void zip(OutputStream out, List<FileEntry> fileEntrys) {
new ZipUtils(out, fileEntrys, null);
} /**
* 根据源目录 制作压缩包
* @param srcFiles 源文件目录
* @param out 输出文件流
* @param filter 文件过滤,不过滤可以为null。
* @param parent 压缩包根目录名,不需要可以为null。
* @param prefix 文件前缀,不需要可以为null。
* @param encoding 编码 ,不设置取系统编码。
*/ public static void zip(File[] srcFiles, OutputStream out, FilenameFilter filter, String parent, String prefix, String encoding) {
Assert.notEmpty(srcFiles);
List<FileEntry> fileEntrys = new ArrayList<FileEntry>();
for (int i = 0; i < srcFiles.length; i++) {
FileEntry fileEntry = new FileEntry(parent, prefix, srcFiles[i], filter);
fileEntrys.add(fileEntry);
}
new ZipUtils(out, fileEntrys, encoding);
} /**
* 创建ZipUtils对象
* @param out 输出流
* @param filter 文件过滤,不过滤可以为null。
* @param fileEntrys 源文件名。可以有多个源文件,如果源文件是目录,那么所有子目录都将被包含。
*/
protected ZipUtils(OutputStream out, List<FileEntry> fileEntrys, String encoding) {
Assert.notEmpty(fileEntrys);
long begin = System.currentTimeMillis();
log.debug("开始制作压缩包");
try {
try {
zipOut = new ZipOutputStream(out);
if (!StringUtils.isBlank(encoding)) {
log.debug("using encoding: {}", encoding);
zipOut.setEncoding(encoding);
} else {
log.debug("using default encoding");
}
for (FileEntry fe : fileEntrys) {
zip(fe.getFile(), fe.getFilter(), fe.getZipEntry(), fe.getPrefix());
}
} finally {
zipOut.close();
}
} catch (IOException e) {
throw new RuntimeException("制作压缩包时,出现IO异常!", e);
}
long end = System.currentTimeMillis();
log.info("制作压缩包成功。耗时:{}ms。", end - begin);
} /**
* 压缩文件
* @param srcFile 源文件
* @param pentry 压缩包根目录名,不需要可以为null。
* @param prefix 文件前缀
* @throws IOException
*/
private void zip(File srcFile, FilenameFilter filter, ZipEntry pentry, String prefix) throws IOException {
ZipEntry entry;
if (srcFile.isDirectory()) {
if (pentry == null) {
entry = new ZipEntry(srcFile.getName());
} else {
entry = new ZipEntry(pentry.getName() + "/" + srcFile.getName());
}
File[] files = srcFile.listFiles(filter);
for (File f : files) {
zip(f, filter, entry, prefix);
}
} else {
if (pentry == null) {
entry = new ZipEntry(prefix + srcFile.getName());
} else {
entry = new ZipEntry(pentry.getName() + "/" + prefix + srcFile.getName());
}
FileInputStream in;
try {
log.debug("读取文件:{}", srcFile.getAbsolutePath());
in = new FileInputStream(srcFile);
try {
zipOut.putNextEntry(entry);
int len;
while ((len = in.read(buf)) > 0) {
zipOut.write(buf, 0, len);
}
zipOut.closeEntry();
} finally {
in.close();
}
} catch (FileNotFoundException e) {
throw new RuntimeException("制作压缩包时,源文件不存在:" + srcFile.getAbsolutePath(), e);
}
}
} /**
* 解压
* @param zipFile 压缩包文件
* @param destDir 压缩路径
* @param encoding
* @author Sun Qinbo
*/
public static void unzip(File zipFile, File destDir, String encoding) {
long begin = System.currentTimeMillis();
log.debug("开始解压缩包");
if (destDir.exists() && !destDir.isDirectory()) {
throw new IllegalArgumentException("destDir is not a directory!");
}
ZipFile zip = null;
InputStream is = null;
FileOutputStream fos = null;
File file;
String name;
int readed;
ZipEntry entry;
try {
try {
if (StringUtils.isNotBlank(encoding)) {
zip = new ZipFile(zipFile, encoding);
} else {
zip = new ZipFile(zipFile);
}
Enumeration<?> en = zip.getEntries();
while (en.hasMoreElements()) {
entry = (ZipEntry) en.nextElement();
name = entry.getName();
name = name.replace('/', File.separatorChar);
file = new File(destDir, name);
if (entry.isDirectory()) {
file.mkdirs();
} else {
// 创建父目录
file.getParentFile().mkdirs();
is = zip.getInputStream(entry);
fos = new FileOutputStream(file);
while ((readed = is.read(buf)) > 0) {
fos.write(buf, 0, readed);
}
fos.close();
is.close();
}
}
} finally {
if (fos != null) {
fos.close();
}
if (is != null) {
is.close();
}
if (zip != null) {
zip.close();
}
}
} catch (IOException e) {
log.error("", e);
}
long end = System.currentTimeMillis();
log.info("解压缩包成功。耗时:{}ms。", end - begin); } /** 测试 */
public static void main(String[] args) throws IOException {
List<FileEntry> fileEntrys = new ArrayList<FileEntry>();
File[] listFiles = new File("d://test").listFiles();
for (int i = 0; i < listFiles.length; i++) {
fileEntrys.add(new FileEntry("", "", listFiles[i]));
}
ZipUtils.zip(new FileOutputStream("D://测试_1.zip"), fileEntrys);
ZipUtils.zip(new File("d://test").listFiles(), new FileOutputStream("D://测试_2.zip"), null, "自定义根目录", "自定义前缀_", "UTF-8");
ZipUtils.unzip(new File("D://测试_2.zip"), new File("D://测试_2"), null);
} /**
* 源文件自定义类型
*/
public static class FileEntry {
private FilenameFilter filter;
private String parent; // 压缩包内的目录名
private File file;
private String prefix; // 压缩文件前缀 public FileEntry(String parent, String prefix, File file, FilenameFilter filter) {
this.parent = parent;
this.prefix = prefix;
this.file = file;
this.filter = filter;
} /**
* @param parent 压缩包内的目录名
* @param file 压缩文件前缀
*/
public FileEntry(String parent, File file) {
this.parent = parent;
this.file = file;
} public FileEntry(String parent, String prefix, File file) {
this(parent, prefix, file, null);
} public ZipEntry getZipEntry() {
if (StringUtils.isBlank(parent)) {
return null;
} else {
return new ZipEntry(parent);
}
} public FilenameFilter getFilter() {
return filter;
} public void setFilter(FilenameFilter filter) {
this.filter = filter;
} public String getParent() {
return parent;
} public void setParent(String parent) {
this.parent = parent;
} public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} public String getPrefix() {
if (prefix == null) {
return "";
} else {
return prefix;
}
} public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
}
java zip工具类的更多相关文章
- java zip 工具类
原文:http://www.open-open.com/code/view/1430906539866 package com.topsoft.websites.utils; import java. ...
- java常用工具类(二)
1.FtpUtil package com.itjh.javaUtil; import java.io.File; import java.io.FileOutputStream; import ja ...
- Java Properties工具类详解
1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...
- Java json工具类,jackson工具类,ObjectMapper工具类
Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
- Java并发工具类 - CountDownLatch
Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...
- MinerUtil.java 爬虫工具类
MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...
- MinerDB.java 数据库工具类
MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...
- 小记Java时间工具类
小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...
随机推荐
- Opensuse13.2开启ssh
要开启ssh很简单,步骤分为三步 步骤1: 安装ssh zypper install ssh 步骤2:启动ssh daemon网上很多通过 service 命令或者 init.d 目录中的脚本启动ss ...
- 不用派生CTreeCtrl不用繁琐的过程 教你如何让CTreeCtrl的每一项有ToolTip提示
最近工作中需要让CTreeCtrl控件的每一项都有提示信息,于是谷歌百度,爬山涉水,结果是………….在CodeProject里找到一篇文章是把CTreeCtrl派生出新类,重载一些函数自定义内容.使用 ...
- 转载--eclipse git插件安装
原文地址:http://yufenfei.iteye.com/blog/1750124/ 一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2 ...
- AIX下解决POWERHA的脑裂问题
一.安装创建并发vg时必需的软件包clvm包,该包安装.升级.后必须重启os clvm包的描述:Enhanced Concurrent Logical Volume Manager 软件包在aix61 ...
- HTML5简单入门系列(一)
前言 随着HTML5的流行,LZ作为一个web开发者,也决定学习一下前端前沿技术. HTML5 是下一代的HTML,它将成为 HTML.XHTML 以及 HTML DOM 的新标准.它是W3C( Wo ...
- github多人协作
1.字符串处理(编码原理) git clone git@github.com:lookphp/LaravelCms.git git add . git commit -m "修改的内容-需要 ...
- 关于sencha touch 的JSONP跨域请求的学习研究
此篇文章是对自己在研究学习sencha touch的过程中的点滴记录,主要是JSONP的跨域请求这方面,对于何为是跨域概念还有不熟悉的,可以自己问下度娘. 先上张图: 我要完成的功能就是表格下拉刷新, ...
- PowerShell常用的属性
get-location | get-member -membertype property -------获取对象的属性---------- 获取对象特定的成员, 湖区.Net Framwork ...
- PowerShell_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念
PowerShell_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念 据我个人所知,windows下的cmd shell除了能够通过修改系统参数来对其中的环境变量 ...
- MySQL流程控制函数
官方文档:Control Flow Functions Name Description CASE Case operator IF() If/else construct IFNULL() Null ...