【转】Java压缩和解压文件工具类ZipUtil
package com.utility.zip; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import com.utility.io.IOUtil; /**
* 通过Java的Zip输入输出流实现压缩和解压文件
*
* @author liujiduo
*
*/
public final class ZipUtil { private ZipUtil() {
// empty
} /**
* 压缩文件
*
* @param filePath
* 待压缩的文件路径
* @return 压缩后的文件
*/
public static File zip(String filePath) {
File target = null;
File source = new File(filePath);
if (source.exists()) {
// 压缩文件名=源文件名.zip
String zipName = source.getName() + ".zip";
target = new File(source.getParent(), zipName);
if (target.exists()) {
target.delete(); // 删除旧的文件
}
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(target);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
// 添加对应的文件Entry
addEntry("/", source, zos);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtil.closeQuietly(zos, fos);
}
}
return target;
} /**
* 扫描添加文件Entry
*
* @param base
* 基路径
*
* @param source
* 源文件
* @param zos
* Zip文件输出流
* @throws IOException
*/
private static void addEntry(String base, File source, ZipOutputStream zos)
throws IOException {
// 按目录分级,形如:/aaa/bbb.txt
String entry = base + source.getName();
if (source.isDirectory()) {
for (File file : source.listFiles()) {
// 递归列出目录下的所有文件,添加文件Entry
addEntry(entry + "/", file, zos);
}
} else {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
byte[] buffer = new byte[1024 * 10];
fis = new FileInputStream(source);
bis = new BufferedInputStream(fis, buffer.length);
int read = 0;
zos.putNextEntry(new ZipEntry(entry));
while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
zos.write(buffer, 0, read);
}
zos.closeEntry();
} finally {
IOUtil.closeQuietly(bis, fis);
}
}
} /**
* 解压文件
*
* @param filePath
* 压缩文件路径
*/
public static void unzip(String filePath) {
File source = new File(filePath);
if (source.exists()) {
ZipInputStream zis = null;
BufferedOutputStream bos = null;
try {
zis = new ZipInputStream(new FileInputStream(source));
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null
&& !entry.isDirectory()) {
File target = new File(source.getParent(), entry.getName());
if (!target.getParentFile().exists()) {
// 创建文件父目录
target.getParentFile().mkdirs();
}
// 写入文件
bos = new BufferedOutputStream(new FileOutputStream(target));
int read = 0;
byte[] buffer = new byte[1024 * 10];
while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, read);
}
bos.flush();
}
zis.closeEntry();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtil.closeQuietly(zis, bos);
}
}
} public static void main(String[] args) {
String targetPath = "E:\\Win7壁纸";
File file = ZipUtil.zip(targetPath);
System.out.println(file);
ZipUtil.unzip("F:\\Win7壁纸.zip");
}
}
package com.utility.io; import java.io.Closeable;
import java.io.IOException; /**
* IO流工具类
*
* @author liujiduo
*
*/
public class IOUtil {
/**
* 关闭一个或多个流对象
*
* @param closeables
* 可关闭的流对象列表
* @throws IOException
*/
public static void close(Closeable... closeables) throws IOException {
if (closeables != null) {
for (Closeable closeable : closeables) {
if (closeable != null) {
closeable.close();
}
}
}
} /**
* 关闭一个或多个流对象
*
* @param closeables
* 可关闭的流对象列表
*/
public static void closeQuietly(Closeable... closeables) {
try {
close(closeables);
} catch (IOException e) {
// do nothing
}
} }
参考网站
【转】Java压缩和解压文件工具类ZipUtil的更多相关文章
- C#压缩和解压文件
这里用两种方法实现C#压缩和解压文件 1.使用System.IO.Compression名称空间下的相关类(需引用 System.IO.Compression.FileSystem和System.IO ...
- java压缩和解压字符串,Byte数组,String
在网上找到的压缩解压的工具类,可以压缩String字符串 /*** * 压缩GZip * * @param data * @return */ public static byte[] gZip(by ...
- java 压缩和解压zip包
网上有关压缩和解压zip包的博文一大堆,我随便找了一个.看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正: package com.wangpeng.utill; import ja ...
- Java 多文件压缩成一个文件工具类
简单修改来自博客园勇闯天涯zfc的博客 一.内容 ①使用 Java 将多个文件打包压缩成一个压缩文件: ②主要使用 java.io 下的类 二.源代码:ZIPUtil .java import jav ...
- 使用GZipStream压缩和解压文件
最近做了一个用.NET里的GZipStream压缩解压缩gzip文件的小程序. GZipStream在System.IO.Compression底下,使用起来也很简单.虽然GZipStream是Str ...
- 压缩和解压文件:tar gzip bzip2 compress(转)
tar[必要参数][选择参数][文件] 压缩:tar -czvf filename.tar.gz targetfile解压:tar -zxvf filename.tar.gz参数说明: -c 建立新的 ...
- linux 压缩和解压文件
一.压缩:20120715文件下面所有的文件 如下: tar -zcvf 20120715.tar.gz 20120715* 二.解压20120715.tar.gz压缩包 如下: tar -xzvf ...
- C# 压缩和解压文件(SharpZipLib)
先从网上下载ICSharpCode.SharpZipLib.dll类库 将文件或文件夹压缩为zip,函数如下 /// <summary> /// 压缩文件 /// </summary ...
- c#调用WinRAR软件压缩和解压文件
using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...
随机推荐
- node工具之pm2
pm2 PM2是带有内置负载平衡器的Node.js应用程序的生产过程管理器.它使您可以使应用程序永远保持活动状态,无需停机即可重新加载它们,并简化常见的系统管理任务. 安装 npm install p ...
- 数据绑定-POJO对象绑定参数
测试: 效果:
- Action注入错误
2016-07-13 13:52:09,584 [ERROR]-[com.opensymphony.xwork2.util.logging.commons.CommonsLogger:38] Exce ...
- openlayers之地图截图
方法1 //this.map._this为初始化地图对象 this.map._this.once('postcompose', function (event) { var canvas = even ...
- 第二篇 HTML 常用元素及属性值
常用元素及属性值 先和同学了解下,一部分常用的元素,区别以及属性,常用在哪里. 标签是由左右尖括号抱起来的,由开始标签开始,再由结束标签结束,里面内容则是元素,比如:<div>< ...
- 黑客正在使用美国NSA泄露的工具进行挖矿
早些年我们知道美国国家安全局囤积不少漏洞准备自己使用,结果这些漏洞以及利用工具被方程式组织获得. 随后名为影子经纪人的黑客组织获得这些漏洞和工具后又再次出售,当初的永恒之蓝漏洞就是从这里泄露的. 永恒 ...
- 全面解析Java日期时间API
时区 GMT(Greenwich Mean Time):格林尼治时间,格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时(也就是在格林尼治上空最高点时)的时间. UTC(Universal Time ...
- yum 报错3
. Contact the upstream for the repository and get them to fix the problem. . Reconfigure the baseurl ...
- 二值图像连通分量的提取(python+opencv)
算法: 第一步,将图片转换为二值图像A 第二步,创建和A相同大小但是元素都为0的图像B,并复制A到A_copy中 第三步,A中任选一点值为255的像素,设为p1,并使用计算连通分量算法,当算法收敛时, ...
- Gym - 102082G What Goes Up Must Come Down (树状数组+贪心)
题意:有一个长度为n的序列,你每次可以选择两个相邻的元素交换,求把这个序列排成单峰序列的最少交换次数. 方法一:将元素按数值从大到小排序(保存原来的位置),把最大的插在中间,剩下的依次往两边放,依次考 ...