public class UnzipUtil {
private static final Logger logger = LoggerFactory.getLogger(CopyFileUtil.class);
/**
* Size of the buffer to read/write data
*/
private static final int BUFFER_SIZE = 4096; /**
* Extracts a zip file specified by the zipFilePath to a directory specified
* by destDirectory (will be created if does not exists)
*
* @param zipFilePath
* @param destDirectory
* @throws IOException
*/
public static void unzip(String zipFilePath, String destDirectory) {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
boolean mkdirs = destDir.mkdirs();
if (!mkdirs) {
logger.error("Call UnzipUtility.unzip,destDir mkdirs is false");
}
}
try {
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
boolean mkdirs = dir.mkdirs();
if (!mkdirs) {
logger.error("Call UnzipUtility.unzip,dir mkdirs is false");
}
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
} catch (IOException e) {
logger.warn("call UnzipUtility.unzip, occur exception. e.getMessage:[{}]", e.getMessage());
}
} /**
* Extracts a zip entry (file entry)
*
* @param zipIn
* @param filePath
* @throws IOException
*/
public static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
} }

UnzipUtil的更多相关文章

  1. JAVA利用Zip4j解压缩【转】

    官方地址:http://www.lingala.net/zip4j/(需要FQ) jar包:http://pan.baidu.com/s/145hwI 演示包:http://pan.baidu.com ...

  2. java解压缩zip

    依赖的包: <!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> <dependency> <gr ...

  3. java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)

    // java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...

随机推荐

  1. 蓝桥杯 倍数问题(dfs,枚举组合数)

    标题:倍数问题 [题目描述]众所周知,小葱同学擅长计算,尤其擅长计算一个数是否是另外一个数的倍数.但小葱只擅长两个数的情况,当有很多个数之后就会比较苦恼.现在小葱给了你 n 个数,希望你从这 n 个数 ...

  2. 【mybatis】-- springboot整合mybatis

    1.添加依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>m ...

  3. BZOJ-9-3295: [Cqoi2011]动态逆序对

    题意:N个数的排列,M次操作,每次求当前的逆序对数量并删掉一个数 思路 :动态说的很到位.hiahia ... 最初一直没想明白为什么 大佬的cdq 中统计了两次. 先定义 给出的删除的点的 t 值依 ...

  4. mongodb配置问题

    1.安装好mongodb后需要在控制行输入很长的一窜很麻烦,可以新建一个.bat文件 cd F:\mongodb\Server\3.0\binmongod --dbpath "F:\mong ...

  5. Installation of CarbonData 1.1.0 with Spark 1.6.2

    关键词:carbondata spark thrift 数据仓库 [Install thrift 0.9.3] 注意 要装thrift-java必须先装ant . 有人说要装boost,我在cento ...

  6. 浅谈vue性能优化

    基础优化 所谓的基础优化是任何 web 项目都要做的,并且是问题的根源.HTML,CSS,JS 是第一步要优化的点 分别对应到 .vue 文件内的,<template>,<style ...

  7. systemverilog中实现饱和截位和饱和截位的分析

    截位(rnd/prnd/floor):都是去掉低位数据的操作(去掉低位低精度的数据,或者说小数位,降低数据的精度) 饱和(sat/sym_sat):都是去掉高位数据的操作,(去掉无符号数高位的0,或者 ...

  8. Python函数中的列表

    在看21天的Python书中写出了一个陷阱,但没给出解释,以下为代码陷阱

  9. C++ 编译发现 error C2146: syntax error : missing ';' before identifier 'm_ctrlserver'

    解决这个问题的根源是重复包含了头文件

  10. nginx,maven

    nginx反向代理 负载均衡 keepalive高可用 lvs负载均衡算法 mvn build自定义命令 install安装到本地仓库