特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/
 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
}
} }

参考网站

https://www.oschina.net/code/snippet_1021818_48130

【转】Java压缩和解压文件工具类ZipUtil的更多相关文章

  1. C#压缩和解压文件

    这里用两种方法实现C#压缩和解压文件 1.使用System.IO.Compression名称空间下的相关类(需引用 System.IO.Compression.FileSystem和System.IO ...

  2. java压缩和解压字符串,Byte数组,String

    在网上找到的压缩解压的工具类,可以压缩String字符串 /*** * 压缩GZip * * @param data * @return */ public static byte[] gZip(by ...

  3. java 压缩和解压zip包

    网上有关压缩和解压zip包的博文一大堆,我随便找了一个.看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正: package com.wangpeng.utill; import ja ...

  4. Java 多文件压缩成一个文件工具类

    简单修改来自博客园勇闯天涯zfc的博客 一.内容 ①使用 Java 将多个文件打包压缩成一个压缩文件: ②主要使用 java.io 下的类 二.源代码:ZIPUtil .java import jav ...

  5. 使用GZipStream压缩和解压文件

    最近做了一个用.NET里的GZipStream压缩解压缩gzip文件的小程序. GZipStream在System.IO.Compression底下,使用起来也很简单.虽然GZipStream是Str ...

  6. 压缩和解压文件:tar gzip bzip2 compress(转)

    tar[必要参数][选择参数][文件] 压缩:tar -czvf filename.tar.gz targetfile解压:tar -zxvf filename.tar.gz参数说明: -c 建立新的 ...

  7. linux 压缩和解压文件

    一.压缩:20120715文件下面所有的文件 如下: tar -zcvf 20120715.tar.gz  20120715* 二.解压20120715.tar.gz压缩包 如下: tar -xzvf ...

  8. C# 压缩和解压文件(SharpZipLib)

    先从网上下载ICSharpCode.SharpZipLib.dll类库 将文件或文件夹压缩为zip,函数如下 /// <summary> /// 压缩文件 /// </summary ...

  9. c#调用WinRAR软件压缩和解压文件

    using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...

随机推荐

  1. JavaSE--jdbc编程

    JDBC全称为:Java Data Base Connectivity (java数据库连接),可以为多种数据库提供统一的访问.JDBC是sun开发的一套数据库访问编程接口,是一种SQL级的API.它 ...

  2. ajax调用,action返回的中文为乱码的解决方案

    原文:ajax调用,action返回的中文为乱码的解决方案 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.c ...

  3. 强大的开源企业级数据库监控利器Lepus

    Lepus监控简单介绍 官方网站:http://www.lepus.cc 开源企业级数据库监控系统 简洁.直观.强大的开源数据库监控系统,MySQL/Oracle/MongoDB/Redis一站式性能 ...

  4. colspan和rowspan

    colspan和rowspan这两个属性用于创建特殊的表格. colspan用来指定单元格横向跨越的列数:colspan就是合并列的,colspan=2的话就是合并两列. rowspan用来指定单元格 ...

  5. render:h => h(App) ----render函数

    转载其他博客1 new Vue({ 2 3 router, 4 store, 5 //components: { App } vue1.0的写法 6 render: h => h(App) vu ...

  6. 转载:开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别

    转自:https://www.cnblogs.com/findumars/p/6309048.html 首先借用有心人士的一张相当直观清晰的图来划分各种协议:开源许可证GPL.BSD.MIT.Mozi ...

  7. 排序——插入排序(C语言)

    void insertSort(int* a,int T){ int tmp,p; ;i<T;i++){ tmp=a[i]; p=i-; &&tmp<a[p]){ a[p+ ...

  8. 关于join

  9. C语言结构体初始化的四种方法

    定义 struct InitMember{    int first:    double second:    char* third:    float four;}; 方法一:定义时赋值 str ...

  10. java 枚举的用法

    public enum StatisticTableEnum { DOC_BROWSE_STATISTIC("doc_browse_statistic"), DOC_LIB_BRO ...