Zip包解压工具类
最近在做项目的自动检测离线升级,使用到了解压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包解压工具类的更多相关文章
- zip文件解压工具类
java解压zip文件 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...
- 文件压缩、解压工具类。文件压缩格式为zip
package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...
- Windows下安装zip包解压版mysql
Windows下安装zip包解压版mysql 虽然官方提供了非常好的安装文件,但是有的时候不想每次再重装系统之后都要安装一遍MySQL,需要使用zip包版本的MySQL.在安装时需如下三步: 1. 新 ...
- Java_压缩与解压工具类
转载请注明出处:http://blog.csdn.net/y22222ly/article/details/52201675 zip压缩,解压 zip压缩与解压主要依靠java api的两个类: Zi ...
- GZip 压缩解压 工具类 [ GZipUtil ]
片段 1 片段 2 pom.xml <dependency> <groupId>commons-codec</groupId> <artifactId> ...
- java解压缩zip和rar的工具类
package decompress; import java.io.File; import java.io.FileOutputStream; import org.apache.tools.an ...
- 修改jar包配置文件的正确操作,jar包解压出来的文件夹重新打成jar,不依靠开发工具!!!!
修改jar包配置文件的正确操作,有的时候通过一些解压工具可以对内部的文件进行修改,但是有时候会无效.这就很烦了 一.背景: 有一个springboot项目,事先我已经用编译好打成jar包以 ...
- jar与war 包解压
JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是有区别的,JAR包中有一个META-INF\MANIFEST.MF文件,当你找成JAR包时,它会自动生成.JAR包是 ...
- 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 重点: 实现多级子目录的压缩, ...
随机推荐
- tfs项目管理
同一个地址下有多个项目,但同一个文件只能映射一次.有两种方式: 1.只添加一次映射,即只给根目录添加映射,如下图,这样西面的具体的项目就不需要挨个添加了. . 2.每个项目挨个添加映射,使用这种方式要 ...
- vuex教程,vuex使用介绍案例
1.demopageaction: import Vue from "vue"; import Store from "../../store.js"; imp ...
- python使用 openpyxl包 excel读取与写入
'''### 写入操作 ###from openpyxl import Workbook#实例化对象wb=Workbook()#创建表ws1=wb.create_sheet('work',0) #默认 ...
- 说下spring生命周期
面试官:说下spring生命周期 程序员:不会 那你先回去等消息吧 Bean实现了BeanNameAware,Spring会将Bean的ID透传给setBeanName java.后端开发.程 ...
- Optional 的基本用法
参考: https://www.cnblogs.com/xingzc/p/5778090.html http://www.runoob.com/java/java8-optional-class.ht ...
- NIO总结
NIO主要有三大核心部分:Channel(通道),Buffer(缓冲区), Selector.传统IO基于字节流和字符流进行操作,而NIO基于Channel和Buffer(缓冲区)进行操作,数据总是从 ...
- tp5框架中jquery+ajax分页
jaxa分页,点击按钮直接替换数据, //php代码$page=Request::instance()->param("page"); $page = empty($page ...
- Android Studio 3.1.3正式版的新坑。。。
Gradle编译时没问题,运行App时候出现: java.util.NoSuchElementException java.lang.RuntimeException: com.android.bui ...
- 常用curl命令
curl -F "userfile=@/Users/username/Downloads/20170502.zip" http://youip/up.php curl -X POS ...
- 使用chrome浏览器无法访问github提示不是私密连接且无继续前往选项
在hosts文件中添加如下内容: 192.30.253.112 github.com192.30.253.119 gist.github.com151.101.100.133 assets-cdn.g ...