java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例
本文实例通过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
}
}
}
总结
以上就是本文关于Java压缩文件工具类ZipUtil使用方法代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。
java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例相关推荐
- java util zip.zipexc,JAVA解压zip压缩文件的实例
今天在弄一个东西,需要在PL/SQL中解压zip的压缩包,刚开始的时候是想着直接在PLSQL中调用java,在java里面调用unzip的shell命令来解析压缩文件,但是比较悲剧,一直老是失败,在尝 ...
- 一个撇脚的java压缩文件工具类
今天弄里一个压缩文件的工具类,功能不是很完善,只支持压缩后单级目录,二级文件夹的文件名好像只能用中文,并且解压后还是乱码.请各位高手大侠批评指教,不胜感激! package tests; import ...
- Java获取压缩包内文件数_使用java.util.zip.ZipFile,压缩包内文件数量过多则报错问题....
引入的jar包不对,或者你的压缩文件不标准.62616964757a686964616fe78988e69d8331333365646331一般就会报这个错误.关于上面英语的翻译:Key featur ...
- python管理工具ports_Python options.port方法代码示例
本文整理汇总了Python中tornado.options.port方法的典型用法代码示例.如果您正苦于以下问题:Python options.port方法的具体用法?Python options.p ...
- python setup用法_python的构建工具setup.py的方法使用示例
python setup.py怎么写不想跟你说晚安,想挤进你被子里,抱你在怀里,给你一个晚安吻. setup.py是python的自动安装文件; 它的原理是按照命令行的调用方式去执行文件的复制和粘贴; ...
- Java读取.txt文件工具类
相关工具类代码 import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; i ...
- java 中导出word后压缩文件_Java批量导出word压缩后的zip文件案例
一.js代码,由于参数比较大所以利用form表单使用post导出 function export_word(){ var selectedRows = $("#dg").datag ...
- java将链接生成二维码工具类
一.添加依赖 <!-- 生成二维码--><dependency><groupId>com.google.zxing</groupId><artif ...
- java加密文件夹_使用java.util.zip压缩文件夹,支持加密,增加描述
导读热词 下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. import java.io.File; import java.io.File ...
最新文章
- spring-boot-maven-plugin插件的作用
- HTML、CSS知识点总结,浅显易懂。
- print\println\printf的区别
- c语言两个条件同时成立,为什么if的条件成立else内的条件成立两个程序同时执行...
- Maven安装教程详解与导入
- 【Java数据结构】赫夫曼树
- 使用IDEA逆向生成实体类时注意问题(Maven)
- 百度之星2018资格赛t6三原色图(MST minimum spanning tree)
- 变异数分析_人工智能系统中分析变异的祸害
- 苹果mp3软件_第二十一期:喜马拉雅听书x2m格式转换mp3
- 看到大量状态SYN_RCVD的连接,可能发生的原因是什么?
- 项目经理之我思员工能动性
- 从高考到程序员——我一直在寻找答案
- nuc7 android tv,第七代的进化,Intel NUC7i3BNH 开箱评测拆解
- The Elder(hdu 5956 树上斜率dp + 队列还原)
- linux修改用户uid gid
- java手势识别技术_Android基础开发之手势识别
- 《游戏机制——高级游戏设计技术》一1.1 规则定义游戏
- c语言,函数声明的误区
- 简单制作视频画面水平镜像播放特效
热门文章
- 阿里在美申请区块链专利;Win10 最新漏洞被发现;MongoDB 4.2 发布​ | 极客头条...
- 我是如何提升 Rust 编译器的速度?
- IoT 打响安防保卫战!
- Web 组件势必取代前端?
- Google 为什么以 Flutter 作为原生突破口?| 技术头条
- 零经验程序员如何抢占面试机会?
- 2019 最新实战!给程序员的 7 节深度学习必修课,最好还会 Python!
- 不要在爬虫犯罪的边缘疯狂试探!
- 一文看懂 BDTC 2018:探秘大数据新应用(附 PPT 下载)
- winform调用websocket_C#基于websocket的前台及后台实时推送