Java 基础【15】 压缩与解压缩
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】 压缩与解压缩的更多相关文章
- java基础---->Zip压缩的使用(转)
java中提供了对压缩格式的数据流的读写.它们封装到现成的IO 类中,以提供压缩功能.下面我们开始java中压缩文件的使用. 目录导航: 关于压缩的简要说明 GZIP压缩文件的使用 ZIP压缩文件的使 ...
- java基础---->Zip压缩的使用
java中提供了对压缩格式的数据流的读写.它们封装到现成的IO 类中,以提供压缩功能.下面我们开始java中压缩文件的使用. 目录导航: 关于压缩的简要说明 GZIP压缩文件的使用 ZIP压缩文件的使 ...
- 【java基础 15】java代码中“==”和equals的区别
导读:昨夜闲来无事,和贾姑娘聊了聊java基础,然后就说到了这个"=="和equals的问题,我俩都是以前了解过,也常用这个,但是,昨天说到的时候,又乱了,什么比较地址值,什么判断 ...
- Java基础15:深入剖析Java枚举类
更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...
- java基础(15):常用API(Object、String、StringBuffer)
1. Java的API及Object类 在以前的学习过程中,我们都在学习对象基本特征.对象的使用以及对象的关系.接下来我们开始使用对象做事情,那么在使用对象做事情之前,我们要学习一些API中提供的常用 ...
- java生成zip压缩文件,解压缩文件
1.生成zip public static void main(String[] args) { try { // testZip("c:\\temp.txt", "c: ...
- java基础15 内部类(成员内部类、局部内部类)和匿名内部类
一.内部类 1.1.1.成员内部类 一个类定义在另一个类的内部,那么该类就叫作成员内部类 1.1.2.成员内部类访问方式 方式一:在外部类中提供一个方法创建内部类的对象进行访问 方式二:在 ...
- linux压缩、解压缩和归档工具
linux基础之压缩.解压缩和归档工具 1.压缩工具 基本介绍 为了减少文件的原来的文件大小而过多的浪费磁盘的存储空间,我们使用压缩后多文件进行存储 压缩工具的介绍 compress:把文件压缩成以. ...
- Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)
Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...
随机推荐
- 基于Vue、Bootstrap的Tab形式的进度展示
最近基于Vue.Bootstrap做了一个箭头样式的进度展示的单页应用,并且支持了对于一个本地JS文件的检索,通过这个单页应用,对于Vue的理解又深入了一些.在这里把主要的代码分享出来. 本单页应用实 ...
- Logstash使用jdbc_input同步Mysql数据时遇到的空时间SQLException问题
今天在使用Logstash的jdbc_input插件同步Mysql数据时,本来应该能搜索出10条数据,结果在Elasticsearch中只看到了4条,终端中只给出了如下信息 [2017-08-25T1 ...
- 将Linux默认的OpenJDK替换为Oracle JDK
在使用Logstash安装插件的时候,发生了一个错误,如下: ERROR: Something went wrong when installing logstash-input-jdbc, mess ...
- intellij idea 如何一键清除所有断点
原文地址: https://blog.csdn.net/yanziit/article/details/73459795 我之前写了一个百度经验,但是搜不到,现在复制一遍,自己留个记录. 注:此方法适 ...
- maven项目里,junit的test程序不能访问src/test/resource下面的配置
问题描述 最近在写单元测试,但是不想改动源代码,所以想自己在本test目录下建一个resouces文件夹并添加对应的配置文件,可是发现test程序无法读取这个resouces文件夹下的配置. 问题解决 ...
- 基于fasttext的情感分析,准备先做一版
博客文章地址: https://blog.csdn.net/sinat_33741547/article/details/78803766 代码地址: https://github.com/lpty/ ...
- C++ 友元类使用 (friend)
C++中私有变量对外部类是不能直接访问的,也是不能继承的. 使用友元类可以访问类中的私有方法.私有变量,虽然对类的封装有一定的破坏,但是有时也是很实用的. 在实际中,在修改已有代码时,为了不大改动已有 ...
- java的关于流程结构做的几个案例
最近在学习中,做了一个java的几个案例,主要是九九乘法口诀,实心菱形和空心菱形的算法,模拟彩票程序以及BMI的测试标准等小案例. 一:九九乘法表 /** * 九九乘法口诀 */ public sta ...
- asp.net mvc源码分析-ModelValidatorProviders 客户端的验证
几年写过asp.net mvc源码分析-ModelValidatorProviders 当时主要是考虑mvc的流程对,客户端的验证也只是简单的提及了一下,现在我们来仔细看一下客户端的验证. 如图所示, ...
- DOM之通俗易懂讲解
DOM是所有前端开发每天打交道的东西,但是随着jQuery等库的出现,大大简化了DOM操作,导致大家慢慢的“遗忘”了它的本来面貌.不过,要想深入学习前端知识,对DOM的了解是不可或缺的,所以本文力图系 ...