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. CSS---通向臃肿的道路(关于 “separation of concerns” (SoC)的原则)

    When it comes to CSS, I believe that the sacred principle of “separation of concerns” (SoC) has lead ...

  2. IE下CSS3伪类的支持

    当css3.0出现以后,着实让我兴奋了好久,因为出现了很多选择器,我们在也不用靠js做复杂判断了.比如:nth-child,很容易就可以判断奇偶对象 “:nth-child(2n)和:nth-chil ...

  3. __x__(46)0910第六天__框架集

     框架集frameset 和 内联框架iframe 的作用类似: 在一个页面中,引入其他的外部html页面. 框架集可以同时引入多个页面. 在 html5 中,推荐使用框架集,而不推荐使用iframe ...

  4. MyISAM和Innodb区别,为什么?

    事务支持 MyISAM不支持事务,而InnoDB支持. InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度, 所以最好是把多条SQL语句显 ...

  5. Wireshark简单使用教程3——附视频

    视频https://www.bilibili.com/video/av35763613?from=search&seid=10176480091153063668 目录 抓取干净流量包的用处所 ...

  6. page1201未完成

    import java.util.Scanner; /** * @author:李柏宏(LiberHome) * @date:Created in 2019/3/4 20:37 * @descript ...

  7. MacBook Air 装win10系统 by DODUI

    为了给齐哥更完美的体验Windows10系统,DODUI亲手操刀MacBook双系统安装Win10,双系统安装教程如下: 终于遇到各种奇葩问题,给小伙伴分享一下. 双系统安装Win10准备工具: 1. ...

  8. 秒杀ecshop的前台写shell 0day

    ECSHOP号称最大的开源网店系统,官方是这样介绍它的:“ECShop网店系统是一套免费开源的网上商店软件,无论在稳定性.代码优化.运行效率.负载能力.安全等级.功能可操控性和权限严密性等方面都居国内 ...

  9. fatal error LNK1120: 11 unresolved externals

    一般原因是函数声明没有定义,或者c++文件没有包含include进来

  10. PHP算法学习(6) 单向链表 实现栈

    svn地址:svn://gitee.com/zxadmin/live_z 这个是模拟栈的先进后出的一个链表操作,自动维护链表,当然你也使用SPL的栈 测试版本php 5.4 ,5.6,7.0,7.2 ...