【转】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 ...
随机推荐
- nginx(五)- linux下安装nginx与配置
linux系统为Centos 64位 准备目录 [root@instance-3lm099to ~]# mkdir /usr/local/nginx [root@instance-3lm099to ~ ...
- Vue中如何插入m3u8格式视频,3分钟学会!
大家都知道video只支持ogg.webm.MP4格式,但是要是m3u8格式的视频怎么办?最近遇到这个问题在网上找了好多办法都不行,最后找到video.js后才完美解决,所以决定写一 ...
- Excel中的常用快捷键
1)工作表之间快速切换 Ctrl+PageUp切换的是当前所在工作表的前一个工作表, Ctrl+PageDown切换的是当前所在工作表的后一个工作表. 2)Ctrl +Home 强迫回到最前一个单元格 ...
- 【转】CNN+BLSTM+CTC的验证码识别从训练到部署
[转]CNN+BLSTM+CTC的验证码识别从训练到部署 转载地址:https://www.jianshu.com/p/80ef04b16efc 项目地址:https://github.com/ker ...
- iOS获取APP的版本号和名称
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...
- 1、linux基础优化
1.添加一个用户 [root@oldboy ~]# useradd oldboy [root@oldboy ~]# id oldboy uid=500(oldboy) gid=500 (oldboy) ...
- strings、strconv:让你高效的处理字符串
strings包 strings.HasPrefix(s, prefix string) bool 判断字符串s是否以prefix开头.类似于python中的startswith. package m ...
- 移远模组-BC95-工作模式之间关系
三种连接状态下,均可发送上行数据( CoAP/UDP): IDLE 下发送数据, 模块会进入 CONNECT 状态: PSM 下发送是数据会唤醒模块, 进入 CONNECT,或者当 TAU(TAU 的 ...
- l洛谷P4779 【模板】单源最短路径(标准版)(dijkstra)
题目描述 给定一个 NN 个点,MM 条有向边的带非负权图,请你计算从 SS 出发,到每个点的距离. 数据保证你能从 SS 出发到任意点. 输入格式 第一行为三个正整数 N, M, SN,M,S. 第 ...
- hihocoder1384/CH0601 Genius ACM[贪心+倍增+归并排序]
提交地址. 关于lyd给的倍增方法,即从当前枚举向后的$2^k$长度($k$从$1$开始),如果可行就将$k$加一以扩大范围,不可行时将范围不断减半直至$0$. 举个例子,假设当下在1,目标答案是13 ...