压缩工具类 - 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 ...
随机推荐
- Google Guava学习笔记——基础工具类针对Object类的使用
Guava 提供了一系列针对Object操作的方法. 1. toString方法 为了方便调试重写toString()方法是很有必要的,但写起来比较无聊,不管如何,Objects类提供了toStrin ...
- BZOJ1143 [CTSC2008] 祭祀river
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1143 题目大意: 给你n个点,点与点之间由有向边相连.如果u能到达v的话,那么他们就不能同 ...
- Noip2008双栈排序
[问题描述] 用两个栈使一个1...n的排列变得有序.一共有四个操作: A.stack1.push() 读入一个放入栈一 B.stack1.pop() 弹出栈一放入输出序列 C.stack2.push ...
- java 验证日期
- Lessons learned from manually classifying CIFAR-10
Lessons learned from manually classifying CIFAR-10 Apr 27, 2011 CIFAR-10 Note, this post is from 201 ...
- 浏览器解析HTML文档的资源并下载
<img />,<style>这些资源是并行请求与加载. <script>脚本是同步请求与加载,阻塞加载.加载完成并执行后再继续解析HTML. 动态<scri ...
- YARN-RPC
运行在YARN平台上面的RPC. 当前存在非常多的开源RPC框架,比较著名的有Thrift.Protocol Buffers 和 AVRO.他们均有两部分构成:对象序列化和远程过程调用. 重要类: Y ...
- 本地安装xssing
本地安装xssing 环境为apache+mysql+php,linux下和Windows均进行了尝试. 步骤: (1)Mysql中创建xing数据库,将xssing.sql中的语句复制并在xing数 ...
- 压测2.0:云压测 + APM = 端到端压测解决方案
从压力测试说起 压力测试是确立系统稳定性的一种测试方法,通常在系统正常运作范围之外进行,以考察其功能极限和隐患.与功能测试不同,压测是以软件响应速度为测试目标的,尤其是针对在较短时间内大量并发用户的访 ...
- 【mysql5.6】SQL基础
我买了本深入浅出MySQL, 记录一下笔记. 一.数据定义语言(DDL) 1.创建数据库 create database name; 2.显示所有的数据库 show databases; 3.选择 ...