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. [Nowcoder212D]禁书目录_概率期望

    禁书目录 题目大意:清教需要定期给Index清除记忆,在此之前需要把当中的十万三千本禁书取出来......不幸的是,禁书一旦离开了Index就非常脆弱,具体来说,每一本禁书都有一个魔力值 ai ,其记 ...

  2. 【Python】【demo实验32】【回文数的确认】

    原题: 我的代码: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- #判断一个数字是否为回文数 即 12345654321 x = ...

  3. YAPTCHA(HDU2973)【威尔逊定理】

    威尔逊原理.即对于素数p,有(p-1)!=-1( mod p). 首先,将原式变形为[ (3×k+6)! % (3×k+7) + 1] / (3×k+7),所以: 1.3×k+7是素数,结果为1, 2 ...

  4. C++ MinGW 配合 Sublime Text 搭建

    本文主旨 使用MinGW 和 文本编辑器 Sublime Text,来搭建c++编译的平台. Sublime Text 安装 和 解除限制 http://rainss.cn/essay/1124.ht ...

  5. # Clion中编译多个cpp(实现单文件编译)

    Clion中编译多个cpp(实现单文件编译) 在不做任何配置情况下,Clion工程下只能有一个main()函数,新建多个cpp会导致报main()函数重复定义的错误,所以默认情况下无法在一个工程下编译 ...

  6. 模仿ORM

    ORM 对象关系映射 类 ---------->映射--------->    表 对象 ------>映射---------->   一条记录 对象点属性 --->映射 ...

  7. css多种方式实现等宽布局

    本文讲的等宽布局是在不手动设置元素宽度的情况下,使用纯css实现各个元素宽度都相当的效果. 1.使用table-cell实现(兼容ie8) <style> body,div{ margin ...

  8. vue配置外放generate-asset-webpack-plugin

    解决方法:(共有2个方法) 1.借助插件  generate-asset-webpack-plugin .在webpack.prod.conf.js中去生成configServer.json文件,让其 ...

  9. 牛客 109 C 操作数 (组合数学)

    给定长度为n的数组a,定义一次操作为:1. 算出长度为n的数组s,使得si= (a[1] + a[2] + ... + a[i]) mod 1,000,000,007:2. 执行a = s:现在问k次 ...

  10. PMP - 风险识别之风险登记册

    目录 PMP - 风险识别之风险登记册 1. 风险登记册 1.1 已识别风险的清单 1.2 潜在风险责任人 1.3 潜在风险应对措施清单 2. 相关习题 2.1 风险发生的时候,要实施 风险登记册 上 ...