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 重点: 实现多级子目录的压缩, ...
随机推荐
- 第27课 可变参数模板(8)_TupleHelper
1. TupleHelper的主要功能 (1)打印:由于tuple中的元素是可变参数模板,外部并不知道内部到底是什么数据,有时调试时需要知道其具体值,希望能打印出tuple中所有的元素值. (2)根据 ...
- Cache基本原理之:结构
转载自:https://www.jianshu.com/p/2b51b981fcaf Cache entries 数据在主存和缓存之间以固定大小的”块(block)”为单位传递,也就是每次从main ...
- NoSql图形数据库
NoSQL数据库可以按照它们的数据模型分成4类: 键-值存储库(Key-Value-stores); BigTable实现(BigTable-implementations); 文档库(Documen ...
- java效率取随机不重复数
//效率取随机不重复数 public int[] takeRandom(int num) { Random rd = new Random(); int[] rds = new int[num];// ...
- leetCode21: 合并两个有序列表
将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2- ...
- Redis详解入门篇
Redis详解入门篇 [本教程目录] 1.redis是什么2.redis的作者3.谁在使用redis4.学会安装redis5.学会启动redis6.使用redis客户端7.redis数据结构 – 简介 ...
- LAMP架构
LAMP(linux,apache,mysql,php)是linux系统下常用的网站架构模型,用来运行PHP网站.(这得apache是httpd服务),这些服务可以安装同意主机上,也可以安装不同主机上 ...
- <转>ajax 同步异步问题
原文:https://blog.csdn.net/wxr15732623310/article/details/76387787 看似简单明白的问题说起来却迷迷糊糊,完全没有逻辑,说不到重点上,再来整 ...
- SSM商城项目(九)
1. 学习计划 1.Activemq整合springMQ的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. ...
- mysql学习笔记--数据库单表查询
一.查询语句 1. select [选项] 列名 [from 表名] [where 条件] [order by 排序] [group by 分组] [having 条件] [limit 限 ...