Java压缩文件工具类ZipUtil使用方法代码示例
作者:mao2080 发布时间:2022-11-26 02:21:32
标签:java,压缩文件,工具类
本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下:
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");
}
}
下面是通过IO流工具类实现关闭一个或多个流对象的Java语言描述,获取可关闭的流对象列表,具体如下:
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.cnblogs.com/mao2080/p/7435268.html


猜你喜欢
- 前言BeanPostProcessor是一个工厂钩子,允许Spring框架在新创建Bean实例时对其进行定制化修改。例如:通过检查其标注的接
- 一、文件1、基本解释(1)什么是文件?文件是保存数据的地方,比如大家经常使用的word文档、txt文件、excel文件等都是文件。它还可以保
- shrio是一个比较轻量级的安全框架,主要的作用是在后端承担认证和授权的工作。今天就讲一下shrio进行认证的一个过程。首先先介绍一下在认证
- 前言作者:京东科技 张天赐JDK 8 是一次重大的版本升级,新增了非常多的特性,其中之一便是 CompletableFuture。
- #region 提示信息#endregion作用:折叠并隐藏代码 ,别且折叠以后能够显示白字“提示信息”如下图就是使用了#region和#e
- public string NextString(int charLowerBound, int charUpperBound, int l
- 本文实例讲述了Android使用GPS获取用户地理位置并监听位置变化的方法。分享给大家供大家参考,具体如下:LocationActivity
- 最近在学习springmvc,今天把springmvc 参数绑定给整理一下,也算个学习笔记吧!@RequestParam 绑定单个请求Req
- 本文以实例形式讲述了C#单例模式(Singleton Pattern)的实现方法,分享给大家供大家参考。具体实现方法如下:一般来说,当从应用
- 用Canvas画贝塞尔曲线,要画贝塞尔曲线首先了解贝塞尔曲线:由于用计算机画图大部分时间是操作鼠标来掌握线条的路径,与手绘的感觉和效果有很大
- 上一章说明了DataBinding生存的类之间关系,现在这里来看看布局是如何加载的以及view是如何映射的。一、布局加载这里把之前的代码重新
- 本文实例讲述了java实现的DES加密算法。分享给大家供大家参考,具体如下:一、DES加密算法介绍1、要求密钥必须是8个字节,即64bit长
- 在java多线程程序中,所有线程都不允许抛出未捕获的checked exception,也就是说各个线程需要自己把自己的checked ex
- Mybatis Criteria条件查询CriterionCriterion是最基本,最底层的Where条件,用于字段级的筛选。Criter
- 前言:有时候我们在用Spring Aop面向切面编程,需要获取连接点(JoinPoint)方法参数名、参数值。环境:Mac OSXIntel
- 一、对Canvas进行操作对Canvas的一系列操作,是指对Canvas进行旋转、平移、缩放等操作。这些操作可以让Canvas对象使用起来更
- 本文实例为大家分享了Android实现快递物流时间轴效果展示的具体代码,供大家参考,具体内容如下首先,这篇参考了别人的代码。根据自己的项目需
- 后台服务端import java.io.IOException;import java.io.InputStream;import java
- 崩溃来源使用过AIDL进行跨进程通信的同学,肯定遇到过DeadObjectException这个崩溃,那么这个崩溃是怎么来的,我们又该如何解
- 本文实例分析了Android开发之TabHost选项卡及相关疑难解决方法。分享给大家供大家参考,具体如下:前言:虽然现在谷歌已经不推荐使用T