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. Selenium断言的使用,等待

    自动化测试常用断言的使用方法(python) 自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 这 ...

  2. Andy's First Dictionary---set,stringstream

    https://cn.vjudge.net/contest/177260#problem/C stringstream :https://blog.csdn.net/xw20084898/articl ...

  3. Orleans框架简单示范

    希望现在学习Orleans不会晚,毕竟Server Fabric都开源了.本篇博客从Sample的HelloWorld示例程序来解读Orleans的Grains. Server配置参考 : https ...

  4. bzoj 4842 [Neerc2016]Delight for a Cat 最小费用最大流,线性规划

    题意:有n个小时,对于第i个小时,睡觉的愉悦值为si,打隔膜的愉悦值为ei,同时对于任意一段连续的k小时,必须至少有t1时间在睡觉,t2时间在打隔膜.如果要获得的愉悦值尽 量大,求最大的愉悦值和睡觉还 ...

  5. 网络流 最大流—最小割 之SAP算法 详解

    首先引入几个新名词: 1.距离标号: 所谓距离标号 ,就是某个点到汇点的最少的弧的数量(即边权值为1时某个点到汇点的最短路径长度). 设点i的标号为level[i],那么如果将满足level[i]=l ...

  6. Redhat Linux5.4/5.5/5.8/6.0/6.3 ISO镜像文件下载

    版本有RedHat Enterprise Linux(RHEL)5.4/5.5/5.8/6.0/6.3 ISO镜像文件下载地址: RHEL 5.4 ISO下载http://rhel.ieesee.ne ...

  7. 转 mysql Next-Key Locking

    原文:http://dev.mysql.com/doc/refman/5.5/en/innodb-next-key-locking.html 14.5.2.5 Avoiding the Phantom ...

  8. Codechef July Challenge 2018 : Picking Fruit for Chefs

    传送门 好久没写题解了,就过来水两篇. 对于每一个人,考虑一个序列$A$,$A_I$表示当k取值为 i 时的答案. 如果说有两个人,我们可以把$(A+B)^k$二项式展开,这样就发现把两个人合并起来的 ...

  9. help文档制作 chm

    程序中的help文档制作 所用工具:HTML Help Workshop 文件包括:各个html文档,帮助页面的具体内容 hhc文档:help的目录文件 hhk文档:help的索引文件 MAP文件夹中 ...

  10. 重构file_get_contents实现一个带超时POST传值函数

    function wp_file_post_contents($url, $post = null) { $context = array(); if (is_array($post)) { ksor ...