java zip 工具类
原文:http://www.open-open.com/code/view/1430906539866
package com.topsoft.websites.utils; import java.io.*;
import java.util.logging.Logger;
import java.util.zip.*; /**
* Created by sunyameng on 14-3-10.
*/
public class ZipUtil {
private final static Logger logger = Logger.getLogger(ZipUtil.class.getName());
private static final int BUFFER = 1024 * 10;
/**
* 将指定目录压缩到和该目录同名的zip文件,自定义压缩路径
*
* @param sourceFilePath 目标文件路径
* @param zipFilePath 指定zip文件路径
* @return
*/
public static boolean zip(String sourceFilePath, String zipFilePath,String zipFileName) {
boolean result = false;
File source = new File(sourceFilePath);
if (!source.exists()) {
logger.info(sourceFilePath + " doesn't exist.");
return result;
}
if (!source.isDirectory()) {
logger.info(sourceFilePath + " is not a directory.");
return result;
}
File zipFile = new File(zipFilePath + File.separator + zipFileName + ".zip");
if (zipFile.exists()) {
logger.info(zipFile.getName() + " is already exist.");
return result;
} else {
if (!zipFile.getParentFile().exists()) {
if (!zipFile.getParentFile().mkdirs()) {
logger.info("cann't create file " + zipFileName);
return result;
}
}
}
logger.info("creating zip file...");
FileOutputStream dest = null;
ZipOutputStream out = null;
try {
dest = new FileOutputStream(zipFile);
CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
out = new ZipOutputStream(new BufferedOutputStream(checksum));
out.setMethod(ZipOutputStream.DEFLATED);
compress(source, out, source.getName());
result = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (result) {
logger.info("done.");
} else {
logger.info("fail.");
}
return result;
} private static void compress(File file, ZipOutputStream out, String mainFileName) {
int index = file.getAbsolutePath().indexOf(mainFileName);
String entryName = file.getAbsolutePath().substring(index);
//System.out.println(entryName);
if (file.isFile()) {
FileInputStream fi = null;
BufferedInputStream origin = null;
try {
fi = new FileInputStream(file);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(entryName);
out.putNextEntry(entry);
byte[] data = new byte[BUFFER];
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (origin != null) {
try {
origin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else if (file.isDirectory()) {
try {
out.putNextEntry(new ZipEntry(entryName+File.separator));
} catch (IOException e) {
e.printStackTrace();
}
File[] fs = file.listFiles();
if (fs != null && fs.length > 0) {
for (File f : fs) {
compress(f, out, mainFileName);
}
}
}
} /**
* 将zip文件解压到指定的目录,该zip文件必须是使用该类的zip方法压缩的文件
*
* @param zipFile 要解压的zip文件
* @param destPath 指定解压到的目录
* @return
*/
public static boolean unzip(File zipFile, String destPath) {
boolean result = false;
if (!zipFile.exists()) {
logger.info(zipFile.getName() + " doesn't exist.");
return result;
}
File target = new File(destPath);
if (!target.exists()) {
if (!target.mkdirs()) {
logger.info("cann't create file " + target.getName());
return result;
}
}
String mainFileName = zipFile.getName().replace(".zip", "");
File targetFile = new File(destPath + File.separator + mainFileName);
if (targetFile.exists()) {
logger.info(targetFile.getName() + " already exist.");
return result;
}
ZipInputStream zis = null;
logger.info("start unzip file ...");
try {
FileInputStream fis = new FileInputStream(zipFile);
CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
zis = new ZipInputStream(new BufferedInputStream(checksum));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
int count;
byte data[] = new byte[BUFFER];
String entryName = entry.getName();
//logger.info(entryName);
String newEntryName = destPath + File.separator + entryName;
newEntryName=newEntryName.replaceAll("\\\\", "/");
File f = new File(newEntryName);
if(newEntryName.endsWith("/")){
if(!f.exists()){
if(!f.mkdirs()) {
throw new RuntimeException("can't create directory " + f.getName());
}
}
}else{
File temp=f.getParentFile();
if (!temp.exists()) {
if (!temp.mkdirs()) {
throw new RuntimeException("create file " + temp.getName() + " fail");
}
}
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
}
result = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zis != null) {
try {
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (result) {
logger.info("done.");
} else {
logger.info("fail.");
}
return result;
} public static void main(String[] args) throws IOException {
// String path="D:\\temp\\B";
// ZipUtil.zip(path,"d:/temp/c","anhuigs123");
String zipfile ="D:\\temp\\c\\B.zip";
File zipFile = new File(zipfile);
String output="D:\\temp\\c";
ZipUtil.unzip(zipFile, output);
}
}
java zip 工具类的更多相关文章
- java zip工具类
依赖jar :apache-ant-1.9.2-bin.zip import java.io.File; import java.io.FileInputStream; import java.io. ...
- java常用工具类(二)
1.FtpUtil package com.itjh.javaUtil; import java.io.File; import java.io.FileOutputStream; import ja ...
- 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) 获取两个时间之间的日期.月份.年份 ...
随机推荐
- 使用Spring Cloud需要了解一些概念
Spring Cloud是一个基于Spring Boot实现的微服务架构开发工具,它为基于JVM的微服务开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集 ...
- C语言:哲学家吃饭问题
//五个哲学家围坐在一起,两人之间都放有一个叉子,意大利面需要2个叉子吃,哲学家吃饭时候叉子只能拿左右手,哲学家除了吃饭时间其他时间都在思考 #include <stdio.h> #inc ...
- perl 对ENV环境变量的使用
1.hash 方式访问. %ENV key为环境变量名,value为环境变量值 2.调用ENV模块 . use Env qw(PATH); print "path is $ENV{path ...
- uboot顶层mkconfig分析
GNU make:http://www.gnu.org/software/make/manual/make.html#Rules 为了便于理解把uboot中的Makefile配置部分弄出来便于理解,这 ...
- bs4--基本使用
CSS 选择器:BeautifulSoup4 和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. lxml 只会 ...
- 使用Lucene的api将索引创建到索引库中
import org.apache.commons.io.FileUtils; import org.apache.lucene.document.Document; import org.apach ...
- Android自动化测试Uiautomator--UiObject接口简介
UiObject可以理解为控件的对象,主要对对象进行操作.按照一定条件(UiSelector)获取UiObject对象,之后对对象进行相应的操作,如下图所示. 对于对象的操作主要有点击/长按.拖动/滑 ...
- C# 反射总结
反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的 ...
- Python学习案例
例1.求101到200之间所有的质数,并打印总数. 说明:除去1和它本身之外,不能被其他数整除,就是质数. #!/bin/python #-*- coding:utf-8 -*- #使用集合法 l = ...
- [LoadRunner]LR性能测试结果样例分析
R性能测试结果样例分析 测试结果分析 LoadRunner性能测试结果分析是个复杂的过程,通常可以从结果摘要.并发数.平均事务响应时间.每秒点击数.业务成功率.系统资源.网页细分图.Web服务器资源. ...