ZIP解压缩文件的工具类【支持多级目录|全】
ZIP解压缩文件的工具类【支持多级目录|全】
作者:Vashon
网上有很多的加压缩示例代码,但是都只是支持一级目录的操作,如果存在多级目录的话就不行了。本解压缩工具类经过多次检查及重构,最终分享给大家。
package com.ywx.ziputils; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; /**
* ZIP解压缩文件的工具类,文件可以是多级目录的结构进行解压,压缩操作.
* @author yangwenxue(Vashon)
*
*/
public class ZipUtil {
/**
* 压缩文件操作
* @param filePath 要压缩的文件路径
* @param descDir 压缩文件的保存路径
* @throws IOException
*/
public static void zipFiles(String filePath,String descDir) throws IOException{
ZipOutputStream zos=null;
try {
//创建一个zip输出流
zos=new ZipOutputStream(new FileOutputStream(descDir));
//启动压缩
startZip(zos,"",filePath);
System.out.println("=============压缩完毕=============");
} catch (FileNotFoundException e) {
//压缩失败,则删除创建的文件
File zipFile=new File(descDir);
if(zipFile.exists()){
zipFile.delete();
}
e.printStackTrace();
}finally{
if(zos!=null){
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 对目录中所有文件递归遍历进行压缩
* @param zos 压缩输出流
* @param oppositePath 在zip文件中的相对路径
* @param filePath 要压缩的文件路径
* @throws IOException
*/
private static void startZip(ZipOutputStream zos, String oppositePath,
String filePath) throws IOException {
File file=new File(filePath);
if(file.isDirectory()){//如果是压缩目录
File[] files=file.listFiles();//列出所有目录
for(int i=0;i<files.length;i++){
File aFile=files[i];
if(aFile.isDirectory()){//如果是目录,修改相对地址
String newoppositePath=oppositePath+aFile.getName()+"/";
//压缩目录,这是关键,创建一个目录的条目时,需要在目录名后面加多一个"/"
ZipEntry entry=new ZipEntry(newoppositePath);
zos.putNextEntry(entry);
zos.closeEntry();
startZip(zos, newoppositePath, aFile.getPath());
}else{//如果不是目录,则进行压缩
zipFile(zos,oppositePath,aFile);
}
}
}else{//如果是压缩文件,直接调用压缩方法进行压缩
zipFile(zos, oppositePath, file);
}
}
/**
* 压缩单个文件到目录中
* @param zos zip输出流
* @param oppositePath 在zip文件中的相对路径
* @param file 要压缩的文件
*/
private static void zipFile(ZipOutputStream zos, String oppositePath, File file) {
//创建一个zip条目,每个zip条目都必须是相对于跟路径
InputStream is=null; try {
ZipEntry entry=new ZipEntry(oppositePath+file.getName());
//将条目保存到zip压缩文件当中
zos.putNextEntry(entry);
//从文件输入流当中读取数据,并将数据写到输出流当中
is=new FileInputStream(file);
//====这种压缩速度很快
int length=0;
int bufferSize=1024;
byte[] buffer=new byte[bufferSize]; while((length=is.read(buffer, 0, bufferSize))>=0){
zos.write(buffer, 0, length);
} //===============以下压缩速度很慢=================
// int temp=0;
//
// while((temp=is.read())!=-1){
// zos.write(temp);
// }
//==========================================
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 解压文件操作
* @param zipFilePath zip文件路径
* @param descDir 解压出来的文件保存的目录
*/
public static void unZiFiles(String zipFilePath,String descDir){
File zipFile=new File(zipFilePath);
File pathFile=new File(descDir); if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip=null;
InputStream in=null;
OutputStream out=null; try {
zip=new ZipFile(zipFile);
Enumeration<?> entries=zip.entries();
while(entries.hasMoreElements()){
ZipEntry entry=(ZipEntry) entries.nextElement();
String zipEntryName=entry.getName();
in=zip.getInputStream(entry); String outPath=(descDir+"/"+zipEntryName).replace("\\*", "/");
//判断路径是否存在,不存在则创建文件路径
File file=new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经创建,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
out=new FileOutputStream(outPath); byte[] buf=new byte[4*1024];
int len;
while((len=in.read(buf))>=0){
out.write(buf, 0, len);
}
in.close(); System.out.println("==================解压完毕==================");
}
} catch (Exception e) {
System.out.println("==================解压失败==================");
e.printStackTrace();
}finally{
try {
if(zip!=null){
zip.close();
}
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings("static-access")
public static void main(String args[]) throws IOException{
// long startTimes=System.currentTimeMillis();
// new ZipUtil().zipFiles("f:"+File.separator+"Vashon2Xiaoai", "f:"+File.separator+"Vashon2Xiaoai_vvv.zip");
// long times=System.currentTimeMillis()-startTimes;
// System.out.println("耗时:"+times/1000+"秒");
new ZipUtil().unZiFiles("f:"+File.separator+"Vashon2Xiaoai_vvv.zip", "f:"+File.separator+"vvvvss");
}
}
实践出真知,希望广大读者动手操作,如问题可以反馈,共同探讨。。。
版权声明:本文为博主原创文章,未经博主允许不得转载。
ZIP解压缩文件的工具类【支持多级目录|全】的更多相关文章
- ZIP解压缩文件的工具类【支持多级文件夹|全】
ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...
- java将文件打包成ZIP压缩文件的工具类实例
package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- 文件类型工具类:FileTypeUtil
个人学习,仅供参考! package com.example.administrator.filemanager.utils;import java.io.File;/** * 文件类型工具类 * * ...
- Android开发调试日志工具类[支持保存到SD卡]
直接上代码: package com.example.callstatus; import java.io.File; import java.io.FileWriter; import java.i ...
- 写文件的工具类,输出有格式的文件(txt、json/csv)
import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io. ...
- Java 文件切割工具类
Story: 发送MongoDB 管理软件到公司邮箱,工作使用. 1.由于公司邮箱限制附件大小,大文件无法发送,故做此程序用于切割大文件成多个小文件,然后逐个发送. 2.收到小文件之后,再重新组合成原 ...
- Java 压缩文件夹工具类(包含解压)
依赖jar <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons ...
- iOS开发拓展篇—封装音频文件播放工具类
iOS开发拓展篇—封装音频文件播放工具类 一.简单说明 1.关于音乐播放的简单说明 (1)音乐播放用到一个叫做AVAudioPlayer的类 (2)AVAudioPlayer常用方法 加载音乐文件 - ...
- 文件夹工具类 - FolderUtils
文件夹工具类,提供创建完整路径的方法. 源码如下:(点击下载 -FolderUtils.java .commons-io-2.4.jar ) import java.io.File; import o ...
随机推荐
- IntelliJ IDEA 2017 反向代理工具新方法激活
来源:http://blog.lanyus.com/archives/317.html 反向代理工具, 可用于激活JRebel (win64) 1.点击进入 https://github.com/i ...
- 以太坊EVM1.0缺陷
256位的虚拟机 目前主流的CPU是32位或64位,在这些机器上进行256位运算需要将256位分段成多个64位指令执行,执行效率比32/64位低,在存储上方面,保存一个数需要256位的存储空间,绝大多 ...
- sid, pid, gid
(一) 参考 :https://unix.stackexchange.com/questions/18166/what-are-session-leaders-in-ps 命令: ps xao pid ...
- I.MX6 Android busybox 从哪里生成的
/**************************************************************************** * I.MX6 Android busybo ...
- MDZX——张能传
「你们到底要干什么?!」——8012年7月13日 张能于MDZX ———————————— 序章 ———————————— 话说天下大势,分久必合,合久必分. 他肩扛99米大砍刀,站在MDZX大门对面 ...
- JavaScript-Tool:Uploadify-un
ylbtech-JavaScript-Tool:Uploadify 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. http://www.uploadify ...
- Android中shape的使用 (转载)
转自:http://blog.csdn.net/ekeuy/article/details/12349853 在看很多开源代码中都使用到了shape,我看代码的时候一般都一带而过了,没有仔细去研究,这 ...
- [NEXT] 时间管理实践
此文已由作者杨卫强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 我个人认为,浪费时间比较主要的原因有两个 工作缺乏计划 工作过程被打扰,效率低下 以下记录我自己的时间管理实 ...
- 51nod1432【贪心】
对于每个数我找一个和他相加最接近独木舟,然后ans+=1; 想复杂了,直接两端来就好了. 然后两个相加如果<=m那么就让它们在一起,不是的话就让大的一艘船,然后继续搞(贪心) #include ...
- LuoguP1370 Charlie的云笔记序列 【dp】By cellur925
题目传送门 题目大意:给你一个序列,求出它所有区间的本质不同的子序列个数.(空序列也算作本质不同),数据范围$1e5$. 我们肯定是不能一个个枚举区间的...而且这个复杂度下,也就大概$O(n)$或$ ...