import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile; public class ZipUtil { public static final String FILE_SEPARATOR = System
.getProperty("file.separator"); /**
* 将指定的文件解压缩到指定的文件夹,解压后的文件夹目录和给定的压缩文件名相同.
*
* @param zipFilePath
* 全路径
* @param unZipDirectory
* 全路径
* @return 解压缩文件是否成功.
* @throws IOException
*/
public static boolean unZipFile(String zipFilePath, String unZipDirectory)
throws IOException {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<?> entries = zipFile.getEntries();
if (zipFile == null) {
return false;
}
while (entries.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
File f = new File(unZipDirectory + FILE_SEPARATOR
+ zipEntry.getName());
if (zipEntry.isDirectory())
{
if (!f.exists() && !f.mkdirs())
throw new IOException("Couldn't create directory: " + f);
} else {
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
is = new BufferedInputStream(zipFile
.getInputStream(zipEntry));
File destDir = f.getParentFile();
if (!destDir.exists() && !destDir.mkdirs()) {
throw new IOException("Couldn't create dir " + destDir);
}
os = new BufferedOutputStream(new FileOutputStream(f));
int b = -1;
while ((b = is.read()) != -1) {
os.write(b);
}
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
}
}
}
zipFile.close();
return true;
} /**
* 压缩一个文件
* @param filePath
* @param zipPath
* @return
*/
public static boolean zipFile(String filePath,String zipPath){
BufferedReader in=null;
org.apache.tools.zip.ZipOutputStream out=null;
try{
File file=new File(filePath);
in=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"ISO-8859-1"));
FileOutputStream f=new FileOutputStream(zipPath);
CheckedOutputStream ch=new CheckedOutputStream(f,new CRC32());
out=new org.apache.tools.zip.ZipOutputStream(new BufferedOutputStream(ch)); int c;
out.putNextEntry(new org.apache.tools.zip.ZipEntry(file.getName()));
while((c=in.read())!=-1)
out.write(c);
} catch(Exception e){
e.printStackTrace();
return false;
}
finally{
try {
if(in!=null) in.close();
if(out!=null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
} /**
* 压缩一个目录
* @param dir
* @param zipPath
* @return
*/
public static boolean zipDirectory(String dir,String zipPath ){
org.apache.tools.zip.ZipOutputStream out=null;
try{
File dirFile=new File(dir);
if(!dirFile.isDirectory())return false;
FileOutputStream fo=new FileOutputStream(zipPath);
CheckedOutputStream ch=new CheckedOutputStream(fo,new CRC32());
out=new org.apache.tools.zip.ZipOutputStream(new BufferedOutputStream(ch));
zip(out,dirFile,""); }
catch(Exception e){
e.printStackTrace();
return false;
}
finally{
try {
if(out!=null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
} public static void zip(org.apache.tools.zip.ZipOutputStream out,File f,String base)throws Exception{
// System.out.println("Zipping "+f.getName());
if (f.isDirectory()) {
File[] fl=f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base+"/"));
base=base.length()==0?"":base+"/";
for (int i=0;i<fl.length ;i++ ) {
zip(out,fl[i],base+fl[i].getName());
}
}
else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream is=new FileInputStream(f);
BufferedInputStream in = new BufferedInputStream(is);//修改BUG!二进制输出采用buffered
int b;
while ((b=in.read()) != -1)
out.write(b);
in.close();
} } public static void main(String[] args){
// boolean f=zipFile("e:/red100.txt","e:/red.zip");
//boolean f=zipDirectory("e:/red","e:/red2.zip");
try {
unZipFile("D:\\portal_rtb.zip", "D:\\wzb");
//zipDirectory("F:\\list","F:\\list.zip");
} catch (Exception e) {
e.printStackTrace();
} } }

Java-ZipUtil工具类的更多相关文章

  1. Java Properties工具类详解

    1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...

  2. Java json工具类,jackson工具类,ObjectMapper工具类

    Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...

  3. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  4. Java并发工具类 - CountDownLatch

    Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...

  5. MinerUtil.java 爬虫工具类

    MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...

  6. MinerDB.java 数据库工具类

    MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...

  7. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

  8. Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie

    Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...

  9. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  10. java日期工具类DateUtil-续一

    上篇文章中,我为大家分享了下DateUtil第一版源码,但就如同文章中所说,我发现了还存在不完善的地方,所以我又做了优化和扩展. 更新日志: 1.修正当字符串日期风格为MM-dd或yyyy-MM时,若 ...

随机推荐

  1. HDU1814(Peaceful Commission) 【2-SAT DFS暴力求最小字典序的模板】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:给出一个数n,代表有n个党派,每个党派要求派出其中一个人去参加会议,且只能派出一人.给出m ...

  2. 12.Flume的安装

    先把flume包上传并解压 给flume创建一个软链接 给flume配置环境变量 #flume export FLUME_HOME=/opt/modules/flume export PATH=$PA ...

  3. 小菜鸟之servlet

    # Servlet课程-1和2和3 容 Web项目 项目代码部署在服务器上, 一般分为c\s(客户端\服务器端)和b\s(浏览器/服务器) 服务器 常用的服务器(tomcat服务器) tomcat的目 ...

  4. SQLite基础-8.子句(二)

    目录 SQLite子句(二) 1. GROUP BY子句 2. HAVING子句 3. LIMIT 子句 4. IF EXISTS 和 IF NOT EXISTS 子句 SQLite子句(二) 1. ...

  5. 基于Opencv快速实现人脸识别(完整版)

    无耻收藏网页链接: 基于OpenCV快速实现人脸识别:https://blog.csdn.net/beyond9305/article/details/92844258 基于Opencv快速实现人脸识 ...

  6. joda-time使用

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  7. ArrayList与LinkedList的区别,如何减少嵌套循环的使用

    如果要减少嵌套循环的使用: 我们可以将需要在二重循环里面判断的条件放在一个Map的key里面: 在判断的时候只需要进行key是否存在,然后操作接下来的步骤: 这样子就会减少二重循环了,不会发生循环n* ...

  8. Ruby Rails学习中:网站导航,Bootstrap和自定义的CSS,局部视图

    添加一些结构 一.网站导航 1.添加一些结构后的网站布局文件 打开文件:app/views/layouts/application.html.erb 简单介绍一下,添加的代码: 我们从上往下看一下这段 ...

  9. 在Window Server 2016中使用Web Deploy方式发布.NET Web应用

    1.在IIS里面点击获取新的Web平台组件 2.下载Web平台组件并安装 3.在其中搜索Web Deploy,找到3.5版本,并安装 4.继续搜索Web Deploy 3.6版本,并安装 安装好之后, ...

  10. SQL With AS Expression

    A. Creating a simple common table expression The following example shows the total number of sales o ...