压缩工具类 - 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 ...
随机推荐
- 【转载】spring的普通类中如何取session和request对像
原文地址:http://blog.csdn.net/yousite1/article/details/7108585 首先要在web.xml增加如下代码: <listener> <l ...
- short-path problem (Spfa) 分类: ACM TYPE 2014-09-02 00:30 103人阅读 评论(0) 收藏
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #i ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poi生成excel
转自:http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html 1.首先下载poi-3.6-20091214.jar,下载地址如下: ht ...
- Portlet之讲解
Portlet在Web门户上管理和显示的可插拔的用户界面组件.Portlet产生可以聚合到门户页面中的标记语言代码的片段,如HTML,XML等.通常,根据桌面隐喻,一个门户页面显示为一组互相不重叠的p ...
- try-catch语句讲解
try-catch 语句由一个 try 块后跟一个或多个 catch 子句构成,这些子句指定不同的异常处理程序. 引发异常时,公共语言运行时 (CLR) 会查找处理此异常的 catch 语句. 如果当 ...
- Java正则表达式匹配例子
Java正则表达式匹配例子 package com.ibm.test; import java.util.regex.Matcher; import java.util.regex.Pattern; ...
- Chapter 4 持久存储数据对象
1.使用with open("filename.扩展名","r/w/rb/wb") as data代替data=open(..);data.close() 打开 ...
- (转)Linux进程间通信
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 谢谢nonoob纠错 我们在Linux信号基础中已经说明,信号可以看作一种粗糙的进 ...
- SQL技术内幕-5 比较特殊 insert into 数据的写法
---比较特殊,第一次看到这种写法,记录下来 create table Student --学生成绩表 ( id int, --主键 Grade int, --班级 Score int --分数 ) ...