最近在做项目的自动检测离线升级,使用到了解压zip包的操作,本着拿来主义精神,搞了个工具类(同事那边拿的),用着还不错。

package com.winning.polaris.admin.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger; public class ZipUtil {
private static final Logger logger = Logger.getLogger(ZipUtil.class); /**
*
* 解压压缩文件
* @param filePath 目标文件路径
* @param fileName 目标文件名称
* @param decompressionPath 解压到的目录路径
* @return
*/
public static boolean decompressionZipFile(String filePath, String fileName, String decompressionPath) {
boolean result = true;
File file = new File(filePath +File.separator+ fileName);
if(!file.exists()){
logger.info("【解压压缩文件】时间:"+TimeTool.getDateTime()+"文件路径" + filePath +File.separator+ fileName+"文件不存在");
return false;
}
ZipArchiveInputStream zipArchiveInputStream = null;
BufferedOutputStream os=null;
try {
zipArchiveInputStream =new ZipArchiveInputStream(new FileInputStream(file)) ;
ArchiveEntry archiveEntry = null;
String firstFolderName=fileName.substring(0,fileName.indexOf(".zip"));
int count=0;
while ((archiveEntry = zipArchiveInputStream.getNextEntry()) != null) {
logger.info("时间:"+TimeTool.getDateTime()+"解压文件名称:" + archiveEntry.getName());
boolean sign=StringUtils.equals((firstFolderName+"/").toLowerCase(),archiveEntry.getName().toLowerCase());
count++;
if(count==1 && !sign){
decompressionPath=decompressionPath+File.separator+firstFolderName;
}
File outFile =new File(decompressionPath,archiveEntry.getName());
if(archiveEntry.isDirectory()) {
if (!outFile.exists())
outFile.mkdirs();
}else{
try {
os = new BufferedOutputStream(new FileOutputStream(outFile));
IOUtils.copy(zipArchiveInputStream, os);
}finally {
IOUtils.closeQuietly(os);
}
}
}
} catch (Exception ex) {
result = false;
logger.error("【解压更新文件】时间:" + TimeTool.getDateTime() + ",解压文件出错:" + ex.getMessage());
ex.printStackTrace();
}finally {
try {
if (zipArchiveInputStream != null) {
zipArchiveInputStream.close();
}
}catch (Exception e){
//result = false;
logger.error("【解压更新文件】时间:" + TimeTool.getDateTime() + ",关闭流出错:" + e.getMessage());
e.printStackTrace();
}
}
return result;
} public static void main(String[] args) { String filePath = "F:\\hmap111\\HMAP_V2.0";
String fileName = "megrez.zip";
String decompressionPath = "F:\\hmap111\\HMAP_V2.0";
System.out.println(decompressionZipFile(filePath, fileName, decompressionPath));
}
}

Zip包解压工具类的更多相关文章

  1. zip文件解压工具类

    java解压zip文件 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...

  2. 文件压缩、解压工具类。文件压缩格式为zip

    package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...

  3. Windows下安装zip包解压版mysql

    Windows下安装zip包解压版mysql 虽然官方提供了非常好的安装文件,但是有的时候不想每次再重装系统之后都要安装一遍MySQL,需要使用zip包版本的MySQL.在安装时需如下三步: 1. 新 ...

  4. Java_压缩与解压工具类

    转载请注明出处:http://blog.csdn.net/y22222ly/article/details/52201675 zip压缩,解压 zip压缩与解压主要依靠java api的两个类: Zi ...

  5. GZip 压缩解压 工具类 [ GZipUtil ]

    片段 1 片段 2 pom.xml <dependency> <groupId>commons-codec</groupId> <artifactId> ...

  6. java解压缩zip和rar的工具类

    package decompress; import java.io.File; import java.io.FileOutputStream; import org.apache.tools.an ...

  7. 修改jar包配置文件的正确操作,jar包解压出来的文件夹重新打成jar,不依靠开发工具!!!!

    修改jar包配置文件的正确操作,有的时候通过一些解压工具可以对内部的文件进行修改,但是有时候会无效.这就很烦了 一.背景:       有一个springboot项目,事先我已经用编译好打成jar包以 ...

  8. jar与war 包解压

    JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是有区别的,JAR包中有一个META-INF\MANIFEST.MF文件,当你找成JAR包时,它会自动生成.JAR包是 ...

  9. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...

随机推荐

  1. Altmetric

    网站名称: Altmetric 所属国家: 美国 网站分类: 科研软件 网站地址: https://www.altmetric.com Altmetric是一个新兴的指标,字面意思是替代指标,但“社会 ...

  2. python中 函数名加括号与不加括号

    加括号是返回函数的结果,不加括号相当于函数的调用.

  3. Docekr 挂在卷之后访问目录时异常 cannot open directory '.': Permission denied 的解决办法

    1,原因,原因是CentOS7 中的安全模块 selinux 把权限禁掉了 2,解决办法如下 2.1,运行容器是加参数在 --privileged=true   (个人认为这是最佳方式,推荐使用) 如 ...

  4. TCC细读 - 1 例子流程

    http://www.iocoder.cn/categories/TCC-Transaction/ https://github.com/changmingxie/tcc-transaction 细读 ...

  5. 1.MySQL基础

    1.创建一个数据库: create database db_test; 2.创建一个表: MySQL [mysql]> use db_test; Database changed MySQL [ ...

  6. SSO 单点登录

    一.单系统登录机制 1.http无状态协议 web应用采用browser/server架构,http作为通信协议.http是无状态协议,浏览器的每一次请求,服务器会独立处理,不与之前或之后的请求产生关 ...

  7. Could not find a package,configuration file provided by "G2O" ,G2OConfig.cmake,g2o-config.cmake

    因为项目需要使用到g2o,所以自己从git上面clone下来, git clone https://github.com/RainerKuemmerle/g2o.git 然后: cd g2o mkdi ...

  8. BlockTrain网络

    [BlockTrain网络] 1.每个节点都参与全⽹络的路由功能,同时也可能包含其他功能.每个节点都参与验证并传播交易及区块信息,发现并维持与对等节点的连接.在图6-1所⽰的全节点⽤例中,名为“⽹络路 ...

  9. 解决HighChart开发遇到的2个问题

    需求很简单,显示一条24小时的变化曲线 写完代码效果是只有一条直线,连时间轴都没有 第1个错误  Highcharts error #12 当通过要绘制的点超过1000个时就会报这个错,我按分钟计算间 ...

  10. java虚拟机--->>程序计数器

    程序计数器是一块较小的内存空间,他可以看做是当前线程所执行的行号指示器.在虚拟机的概念模型(仅是概念模型,各种虚拟机可能会通过一些更高效率的方式去实现)里,字节码解释器工作室就是通过改变这个计数器的值 ...