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 重点: 实现多级子目录的压缩, ...
随机推荐
- ajax基本原理
- linux服务samba与ftp篇
samba复习: 1.下载samba:yum -y install samba 2.打开配置文件/etc/samba/smb.conf输入: [共享文件名] path = 目录名 (事先创建) pub ...
- k8s学习笔记之一:kubernetes简介
一.虚拟化技术 1.什么是虚拟化技术 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立 ...
- 203. 阿里jetcache
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源码: ...
- 工作流调度器azkaban
为什么需要工作流调度系统 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 各任务单元之间存在时间先后及前后依赖关系 为了很 ...
- leetcode206
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...
- 在ASP.NET MVC中使用UEditor无法提交的解决办法
很简单的一个ajax提交,却怎么都不成功 $.ajax({ type: "POST", url: "/mms/riskmanage/commitreply", ...
- python 安装包制作
1. __init__.py 2.模块1 模块2 3.setup.py from distutils.core import setup setup(name='modules_name',versi ...
- Ueditor 前后端分离实现文件上传到独立服务器
关于Ueditor 前后端分离实现文件上传到独立服务器,在网上搜索确实遇到大坑,不过还好遇到了 虚若影 最终实现了,在此感谢!虚若影的原文博客网址:http://www.cnblogs.com/hpn ...
- SQL Server 2000使用链接服务器
执行:安装盘\SQL2KSP4\install\instcat.sql 文件下载地址:http://download.csdn.net/detail/taomanman/5680765