Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类。

还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类。

依赖 Jdk 编写该工具类,不依赖任何第三方 jar,随用随取,实现功能大体如下:

1.目录级别递归压缩与解压缩 zip;

2.单文件压缩和解压缩 zip ;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; /**
* 通过Java的Zip输入输出流实现压缩和解压文件
*/
public final class ZipUtil { private ZipUtil() { } /**
* 压缩文件
*
* @param filePath 待压缩的文件路径
* @return 压缩后的文件
*/
public static File zip(String filePath) {
File target = null;
File source = new File(filePath);
if (source.exists()) {
String sourceName = source.getName();
String zipName = sourceName.contains(".") ? sourceName.substring(0, sourceName.indexOf(".")) : sourceName;
target = new File(source.getParent(), zipName + ".rar");
if (target.exists()) {
boolean delete = target.delete();//删除旧的压缩包
}
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(target);
zos = new ZipOutputStream(new BufferedOutputStream(fos)); addEntry(null, source, zos); //添加对应的文件Entry
} 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 {
String entry = (base != null) ? base + source.getName() : source.getName(); //按目录分级,形如:aaa/bbb.txt
if (source.isDirectory()) {
File[] files = source.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
addEntry(entry + "/", file, zos);// 递归列出目录下的所有文件,添加文件 Entry
}
}
} 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;
zos.putNextEntry(new ZipEntry(entry)); //如果只是想将文件夹下的所有文件压缩,不需名要压缩父目录,约定文件名长度 entry.substring(length)
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;
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;
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);
}
}
}
}

输入输出 工具类IOUtil :

import java.io.Closeable;
import java.io.IOException; /**
* IO流工具类
*/
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();
}
}
}
} /**
* 关闭一个或多个流对象
*/
public static void closeQuietly(Closeable... closeables) {
try {
close(closeables);
} catch (IOException e) {
// do nothing
}
} }

Java 基础【15】 压缩与解压缩的更多相关文章

  1. java基础---->Zip压缩的使用(转)

    java中提供了对压缩格式的数据流的读写.它们封装到现成的IO 类中,以提供压缩功能.下面我们开始java中压缩文件的使用. 目录导航: 关于压缩的简要说明 GZIP压缩文件的使用 ZIP压缩文件的使 ...

  2. java基础---->Zip压缩的使用

    java中提供了对压缩格式的数据流的读写.它们封装到现成的IO 类中,以提供压缩功能.下面我们开始java中压缩文件的使用. 目录导航: 关于压缩的简要说明 GZIP压缩文件的使用 ZIP压缩文件的使 ...

  3. 【java基础 15】java代码中“==”和equals的区别

    导读:昨夜闲来无事,和贾姑娘聊了聊java基础,然后就说到了这个"=="和equals的问题,我俩都是以前了解过,也常用这个,但是,昨天说到的时候,又乱了,什么比较地址值,什么判断 ...

  4. Java基础15:深入剖析Java枚举类

    更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...

  5. java基础(15):常用API(Object、String、StringBuffer)

    1. Java的API及Object类 在以前的学习过程中,我们都在学习对象基本特征.对象的使用以及对象的关系.接下来我们开始使用对象做事情,那么在使用对象做事情之前,我们要学习一些API中提供的常用 ...

  6. java生成zip压缩文件,解压缩文件

    1.生成zip public static void main(String[] args) { try { // testZip("c:\\temp.txt", "c: ...

  7. java基础15 内部类(成员内部类、局部内部类)和匿名内部类

    一.内部类 1.1.1.成员内部类 一个类定义在另一个类的内部,那么该类就叫作成员内部类 1.1.2.成员内部类访问方式 方式一:在外部类中提供一个方法创建内部类的对象进行访问       方式二:在 ...

  8. linux压缩、解压缩和归档工具

    linux基础之压缩.解压缩和归档工具 1.压缩工具 基本介绍 为了减少文件的原来的文件大小而过多的浪费磁盘的存储空间,我们使用压缩后多文件进行存储 压缩工具的介绍 compress:把文件压缩成以. ...

  9. Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)

    Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...

随机推荐

  1. 双面女间谍第一至五季/全集Alias迅雷下载

    本季第一至五季 Alias Season (2001-2005)看点:<双面女间谍>她在CIA拥有双重身份,是个美貌矫健的年轻女间谍,一个性感的女007.但在第一季中,讲述更多的却是她在间 ...

  2. 福尔摩斯基本演绎法第一季/全集Elementary迅雷下载

    本季Elementary Season 1 第一季(2012)看点:<福尔摩斯:基本演绎法>由CBS出品,根据<福尔摩斯>系列改编,地点从19世纪大英帝国国势鼎盛的时期搬到了2 ...

  3. Bootstrap table的基本使用总结

    最近在学习BootStrap构建页面,现记录BootStrap table 的一些基本使用如下: HTML文件: <!DOCTYPE html> <html> <meta ...

  4. 样条之贝塞尔(Bezier)

    我曾经发过两篇关于贝塞尔的文章:数学图形(1.47)贝塞尔(Bézier)曲线,数学图形之贝塞尔(Bézier)曲面.那是使用我自己定义的脚本语言生成贝塞尔图形.由于我自己定义的脚本语法功能有限,所以 ...

  5. Convert Sorted List to Binary Search Tree leetcode java

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  6. C#操作json类型数据

    将对象序列化为 JavaScript 对象表示法 (JSON),并将 JSON 数据反序列化为对象. 此类不能继承. // msdn 例子: namespace SL_DataContractJson ...

  7. JS的scrollIntoView简单使用

    scrollIntoView方法滚动当前元素,进入浏览器的可见区域 el.scrollIntoView(); // 等同于el.scrollIntoView(true) el.scrollIntoVi ...

  8. logistic回归(一)

    http://www.2cto.com/kf/201307/226576.html , 这个是Sigmoid函数,在这个回归过程中非常重要的函数,主要的算法思想和这个密切相关.这个函数的性质大家可以自 ...

  9. idea缓存目录mac cache

    IDEA如果出现卡顿,Index疯狂扫描,建议清空一下如下目录 ~/Library/Caches/IntelliJIdea2017.3 Resource nexus-maven-repository- ...

  10. PHP Manager for IIS

    SOAP error on IIS8 Registering new PHP version sets bad values set for FastCGI activityTimeout, requ ...