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 ...
随机推荐
- Spring Websocket实现文本、图片、声音、文件下载及推送、接收及显示(集群模式)
相关环境 Nginx,Spring5.x当前(要选择4.0+),tomcat8.x,Quartz 2.x集群(实际运用是Quartz的集群模式和单机模式共存的) 测试面页:http://sms.rey ...
- JavaScript中 location.host 与 location.hostname 的区别
JavaScript 中,大多数情况下,我们不会发现 location.host 与 location.hostname 的区别,因为大多数情况下,我们的网页用的是 80 端口. 他们的区别: loc ...
- 邪恶力量第一至九季/全集Supernatural迅雷下载
邪恶力量 第一季 Supernatural Season 1 (2005) 本季看点:一部充满吸引力的系列剧,超自然现象题材中的亲情与正义.迪恩(简森·阿克斯 Jensen Ackles 饰)和萨姆( ...
- Unable to execute dex: Multiple dex files define 的解决方法
我们在引入library时可能会出现这个错误 比如: [2013-11-05 14:22:15 - Dex Loader] Unable to execute dex: Multiple dex fi ...
- List转换为字符串并添加分隔符
// 方法一: public String listToString(List list, char separator) { StringBuilder sb = new StringBuilder ...
- [转]php curl 设置host curl_setopt CURLOPT_HTTPHEADER 指定host
From : http://digdeeply.org/archives/10132139.html 我们在开发测试时,有时web服务器会绑定一个域名,但是因为dns是无法解析的,我们需要设置host ...
- 【转】Mysql行转换为列
From : http://www.cnblogs.com/lhj588/archive/2012/06/15/2550392.html# 今晚需要统计数据生成简易报表,由原表格数据是单行的形式,最好 ...
- CImg、libjpeg--介绍、配置(操作JPEG)
关于处理图片,之前写了两篇博客关于ImageMagick的: <ImageMagick–介绍> <ImageMagick–VS2015环境配置.开发(registrykeylooku ...
- 解决easyui combobox不能默认选中
开始出现很奇怪的问题,ff没有问题IE8还是会出现不能选中默认项的问题,更改了select.input的方式,数据加载方式也更改为json的方式,未果,最后将datagrid toolbar的初始化放 ...
- 构建配置 Enable multidex
官方文档 配置方法数超过 64K 的应用 随着 Android 平台的持续成长,Android 应用的大小也在增加.当您的应用及其引用的库达到特定大小时,您会遇到构建错误,指明您的应用已达到 Andr ...