Java-ZipUtil工具类
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工具类的更多相关文章
- Java Properties工具类详解
1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...
- Java json工具类,jackson工具类,ObjectMapper工具类
Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
- Java并发工具类 - CountDownLatch
Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...
- MinerUtil.java 爬虫工具类
MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...
- MinerDB.java 数据库工具类
MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...
- 小记Java时间工具类
小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...
- Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie
Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...
- UrlUtils工具类,Java URL工具类,Java URL链接工具类
UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...
- java日期工具类DateUtil-续一
上篇文章中,我为大家分享了下DateUtil第一版源码,但就如同文章中所说,我发现了还存在不完善的地方,所以我又做了优化和扩展. 更新日志: 1.修正当字符串日期风格为MM-dd或yyyy-MM时,若 ...
随机推荐
- [转帖]注解机制(Annotation,区别于comment)
[19/04/16-星期二] 注解机制(Annotation,区别于comment(传统意义上的注释)) 一.概念 作用: ——不是程序本身,可以对程序作出解释.(这一点和注释没什么区别) ——可 ...
- 【Python】【基础知识】【内置常量】
Python的内置常量有: False.True.None.NotImplemented.Ellipsis.__debug__ 由 site 模块添加的常量:quit.exit.copyright.c ...
- 使用winsw包装服务将nginx包装为Windows服务
**Nginx本身在Windows上并不支持以服务的形式运行,官方文件中有提到.http://nginx.org/en/docs/windows.html,所以在Windows下使用winsw将Ngi ...
- PHPRedis教程之geo
前言 支持 GEO 系列命令的 Redis 版本从 3.2.0 起开始才可以使用,所以之前版本就不要想了. 函数列表 geoadd - 将指定的地理空间项(纬度,经度,名称)添加到指定的键, 数据作为 ...
- Linux磁盘挂载、分区、扩容操作
本文最早发布于 Rootrl's blog 注:以下操作系统环境为CentOS7 基本概念 在操作前,首先要了解一些基本概念 磁盘 在Linux系统中所有的设备都会以文件的形式存储.设备一般保存在/d ...
- C# WPF 数据绑定
后台通知: public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(str ...
- python numpy 的用法—— bincount
今天看脚本的时候遇到了几个不懂的用法,记录下来供日后查看: 1.numpy bincount 先上图: 如上所示:首先要求输入的数组不能包含负数: 该函数是计算非负元素的个数,如果数组中的最大值为10 ...
- 与 QWidget 有关的 Qt 可视化组件的继承关系图
与 QWidget 有关的 Qt 可视化组件的继承关系图
- swagger2 Could not resolve pointer: /definitions
错误信息: Errors Resolver error at paths././query.post.parameters.20.schema.$ref Could not resolve refer ...
- iptables防火墙操作-查看、配置、重启、关闭
查看iptables端口配置 iptables -L -n --line-number iptables端口配置(不开通3389无法远程连接,不开通icmp无法ping) iptables -A IN ...