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时,若 ...
随机推荐
- 【转帖】Office的光荣历史(1)
Office的光荣历史(1) https://www.sohu.com/a/201410882_657550 微软的第一版本的office 竟然是 给Mac OS 提供的.. 2017-10-31 1 ...
- Android Application的目录结构
目录结构: 1,java目录:保存java或kotlin源文件 2,res目录:保存Android项目的各种资源文件.比如layout子目录存放界面布局文件,values子目录存放各种XML格式的资源 ...
- 为什么还需要应用层的Keepalive?
既然TCP有了keepalive,应用层还需要Keepalive多此一举吗? 显然是不是的,首先协议分层思想,每层的关注点不同,TCP属于传输层,关注“通”,应用层关注是否能“用”,能“通”不一定能“ ...
- 转 RTSP客户端模拟器(TCP方式,Python实现)
转自: http://www.cnblogs.com/MikeZhang/archive/2012/10/29/rtspTcpClient_DSS_20121029.html 由于某种需求,工作中需要 ...
- Dev-C++的一些使用技巧快捷键
最近开始用Dev-C++在Window下编程,感觉Dec-C++是一款挺不错的C++编译器.下载地址http://www.duote.com/soft/9830.html .现总结一些使用技巧: 1. ...
- docker_nginx_php_mysql
https://blog.csdn.net/miwumuge/article/details/83386679 问题 1.容器内访问宿主机地址, host.docker.internal
- oracle sqlplus执行sql语句字符集问题
因为业务需要,现将一些包含中文的insert语句导入到oracle数据库中,由于数据量比较大,通过pl/sql*plus导入时非常慢(实测1.5M的文件大概执行20分钟),现在oracle服务器sql ...
- spring AOP的相关术语
连接点:Joinpoint 其实业务层接口的方法 切入点:Pointcut 被增强的是切入点,没被增强是永远都是连接点.连接点不一定是切入点,切入点一定是连接点 通知:Advice 就是指要增强的 ...
- mybatis抛出异常(java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1)
文章参考 https://blog.csdn.net/junsure2012/article/details/42171035 https://www.cnblogs.com/WangYunShuai ...
- vue学习(5)-评论功能(利用父组件的方法)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...