压缩工具类 - ZipUtils.java
压缩工具类,提供压缩文件、解压文件的方法。
源码如下:(点击下载 -
ZipUtils.java 、
FolderUtils.java、
ant-1.7.0.jar、
commons-io-2.4.jar、
commons-lang-2.6.jar)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream; /**
* 压缩工具类
*
*/
public class ZipUtils { private static final String DEFAULT_CHARSET = "UTF-8"; /**
* 压缩文件夹
*
* @param zipFileName
* 打包后文件的名称,含路径
* @param sourceFolder
* 需要打包的文件夹或者文件的路径
* @param zipPathName
* 打包目的文件夹名,为空则表示直接打包到根
*/
public static void zip(String zipFileName, String sourceFolder, String zipPathName) throws Exception {
ZipOutputStream out = null;
try {
File zipFile = new File(zipFileName); FolderUtils.mkdirs(zipFile.getParent());
out = new ZipOutputStream(zipFile);
out.setEncoding(DEFAULT_CHARSET);
if (StringUtils.isNotBlank(zipPathName)) {
zipPathName = FilenameUtils.normalizeNoEndSeparator(zipPathName, true) + "/";
} else {
zipPathName = "";
}
zip(out, sourceFolder, zipPathName);
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e);
} finally {
IOUtils.closeQuietly(out);
}
} /**
* 压缩文件夹
*
* @param zipFile
* a {@link java.lang.String} object.
* @param source
* a {@link java.lang.String} object.
*/
public static void zip(String zipFile, String source) throws Exception {
File file = new File(source);
zip(zipFile, source, file.isFile() ? StringUtils.EMPTY : file.getName());
} /**
* 压缩文件夹
*
* @param zipFile
* a {@link java.io.File} object.
* @param source
* a {@link java.io.File} object.
*/
public static void zip(File zipFile, File source) throws Exception {
zip(zipFile.getAbsolutePath(), source.getAbsolutePath());
} private static void zip(ZipOutputStream zos, String file, String pathName) throws IOException {
File file2zip = new File(file);
if (file2zip.isFile()) {
zos.putNextEntry(new ZipEntry(pathName + file2zip.getName()));
IOUtils.copy(new FileInputStream(file2zip.getAbsolutePath()), zos);
zos.flush();
zos.closeEntry();
} else {
File[] files = file2zip.listFiles();
if (ArrayUtils.isNotEmpty(files)) {
for (File f : files) {
if (f.isDirectory()) {
zip(zos, FilenameUtils.normalizeNoEndSeparator(f.getAbsolutePath(), true),
FilenameUtils.normalizeNoEndSeparator(pathName + f.getName(), true) + "/");
} else {
zos.putNextEntry(new ZipEntry(pathName + f.getName()));
IOUtils.copy(new FileInputStream(f.getAbsolutePath()), zos);
zos.flush();
zos.closeEntry();
}
}
}
}
} /**
* 解压
*
* @param fromZipFile
* zip文件路径
* @param unzipPath
* 解压路径
*/
@SuppressWarnings("unchecked")
public static final void unzip(String fromZipFile, String unzipPath) throws Exception { FileOutputStream fos = null;
InputStream is = null;
String path1 = StringUtils.EMPTY;
String tempPath = StringUtils.EMPTY; if (!new File(unzipPath).exists()) {
new File(unzipPath).mkdir();
}
ZipFile zipFile = null;
try {
zipFile = new ZipFile(fromZipFile, DEFAULT_CHARSET);
} catch (IOException e1) {
e1.printStackTrace();
throw new Exception(e1);
}
File temp = new File(unzipPath);
String strPath = temp.getAbsolutePath();
Enumeration<ZipEntry> enu = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enu.hasMoreElements()) {
zipEntry = (ZipEntry) enu.nextElement();
path1 = zipEntry.getName();
if (zipEntry.isDirectory()) {
tempPath = FilenameUtils.normalizeNoEndSeparator(strPath + File.separator + path1, true);
File dir = new File(tempPath);
dir.mkdirs();
continue;
} else { BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
is = zipFile.getInputStream(zipEntry);
bis = new BufferedInputStream(is);
path1 = zipEntry.getName();
tempPath = FilenameUtils.normalizeNoEndSeparator(strPath + File.separator + path1, true);
FolderUtils.mkdirs(new File(tempPath).getParent());
fos = new FileOutputStream(tempPath);
bos = new BufferedOutputStream(fos); IOUtils.copy(bis, bos);
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e);
} finally {
IOUtils.closeQuietly(bis);
IOUtils.closeQuietly(bos);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(fos);
}
}
}
}
}
压缩工具类 - ZipUtils.java的更多相关文章
- Java 实现文件压缩工具类
package com.wdxc.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileI ...
- 【C#】依赖于SharpZipLib的Zip压缩工具类
上班第二天下班,课外作业,实现一个ZIP压缩的工具类.本来想用Package,但是写完了才发现不能解压其他工具压缩的zip包,比较麻烦,因此本工具类依赖了第三方的库(SharpZipLib vers ...
- 最近工作用到压缩,写一个zip压缩工具类
package test; import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream ...
- 使用 Arrays 类操作 Java 中的数组
Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等(关于类和方法的相关内容在后面的章节中会详细讲解滴 ...
- 慕课网-Java入门第一季-6-7 使用 Arrays 类操作 Java 中的数组
来源:http://www.imooc.com/code/1556 Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现 ...
- java能不能自己写一个类叫java.lang.System/String正确答案
原文: http://www.wfuyu.com/php/22254.html 未做测试 ! 最近学习了下java类加载相干的知识.然后看到网上有1道面试题是 能不能自己写个类叫java.lang.S ...
- hadoop中Text类 与 java中String类的区别
hadoop 中 的Text类与java中的String类感觉上用法是相似的,但两者在编码格式和访问方式上还是有些差别的,要说明这个问题,首先得了解几个概念: 字符集: 是一个系统支持的所有抽象字符的 ...
- Arrays 类操作 Java 的数组排序
使用 Arrays 类操作 Java 中的数组 Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等( ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
随机推荐
- 二分---LIGHTOJ 1062
1062 - Crossed Ladders PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB A ...
- poj 3237 Tree 树链剖分
题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...
- 【BZOJ】【2038】小Z的袜子
填个坑吧,学习了莫队算法.我也忘记是看的哪位大牛的博客&代码学习的了T_T,如果您发现了的话请私信我,我会注明学自您的代码. 另外感谢@PoPoQQQ大神 好,进入正文,莫队算法,也算是一种暴 ...
- JAVA 对象数组,加载图片实例 分类: Java Game 2014-08-14 16:57 80人阅读 评论(0) 收藏
主函数: package com.mywork; import java.awt.Color; import java.awt.Image; import javax.swing.ImageIcon; ...
- CIFAR-10 Competition Winners: Interviews with Dr. Ben Graham, Phil Culliton, & Zygmunt Zając
CIFAR-10 Competition Winners: Interviews with Dr. Ben Graham, Phil Culliton, & Zygmunt Zając Dr. ...
- php __FILE__,__CLASS__等魔术变量,及实例(转)
今天看到一个魔术变量,是以前没见过的,__DIR__,我查了查,发现原来是php5.3新增的,顺便举几个例子,解释一下php的魔术变量 1,__FILE__ 文件的完整路径和文件名.如果用在被包含文件 ...
- vs2010创建并使用DLL
一.为什么需要dll 代码复用是提高软件开发 效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架, 如ATL ...
- POJ 1573
#include<iostream> #include<stdio.h> #define MAXN 15 using namespace std; char _m[MAXN][ ...
- Oracle MySQL
http://blog.jobbole.com/46510/ http://blackproof.iteye.com/blog/1570456 http://blog.csdn.net/yzsind/ ...
- ExtJs之Ext.apply
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...