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 重点: 实现多级子目录的压缩, ...
随机推荐
- Struts2 中常用的代码
BaseAction public class BaseAction extends ActionSupport { protected String target; public Map getRe ...
- Vue 封装的noData组件
<template> <div :style="{color: fontColor}" :class="['noDataView', iconType] ...
- ASCS HA
Please let us know what do you mean by "the PAS can not be accessed", what error did you f ...
- IDEA VM设置
1.IDEA vm options -server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128 ...
- TCARS: Time- and Community-Aware Recommendation System(时间感知和社区感知推荐系统)
随着用户在物品上产生了大量行为,推荐系统成为了线上系统的重要组成部分.推荐系统算法使用用户对物品的行为信息以及上下文数据为每个用户推荐一组物品.算法根据用户之间及物品之间的相似度建立.本文介绍了一个基 ...
- Java遍历树(深度优先+广度优先)
在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.深度优先 英文缩写为DFS即Dep ...
- C++ 50学习 之提高对 C++的认识
转自Effective C++ 理解设计目标. 1.和 C 的兼容性. 2.效率. C++在效率上可以和 C 匹 敌 ---- 二者相差大约在 5%之内. 3.和传统开发工具及环境的兼容性. 4.解决 ...
- asmx 接收数据过大 发生错误
原文:https://www.cnblogs.com/xinaixia/p/5945316.html 错误:已超过传入消息(65536)的最大消息大小配额.若要增加配额,请使用相应绑定元素上的 Max ...
- XGBoost参数调优小结
https://mp.weixin.qq.com/s?__biz=MzU0MDQ1NjAzNg==&mid=2247485630&idx=1&sn=9edf2bfd771cf4 ...
- 网络抓包工具 wireshark 入门教程
Wireshark Wireshark(前称Ethereal)是一个网络数据包分析软件.网络数据包分析软件的功能是截取网络数据包,并尽可能显示出最为详细的网络数据包数据.Wireshark使用WinP ...