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. GIT初始学习记录

    目录 GIT学习记录 配置github与gitlib两个账号 基本操作 git init:初始化仓库 git status:查看仓库状态 git add :向缓存区中添加文件 git commit 保 ...

  2. 使php支持pdo_mysql

    1.下载pdo_mysql包 wget https://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz 2.追加编译php,使其module目录下生产pdo_mysql.so ...

  3. Sting、StringBuffer、StringBuilder

    (1)String是字符串常量,一旦创建之后不可更改:StringBuffer和StringBuilder是字符串变量,可以更改.String的不可变,所以适合作为Map的键. (2)StringBu ...

  4. nagios的安装与部署

    参考文献: https://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html https://www.jianshu.com/p/3476d ...

  5. JavaScript继承的几种模式

    原型链 让一个类的原型对象指向另一个类的实例

  6. java学习(四)--- String 、StringBuffer、StringBuilder 和 数组

    对于 String.StringBuffer.StringBuilder比较一下 主要说说三者的不同 String 长度大小不可变 StringBuffer 和 StringBuilder 长度可变 ...

  7. 正则RegExp序2

    1.var reg=/./     var reg=/\./ 前者代表任意一个字符而后面代表这个字符串中得有一个. 2.?的使用 如果单独的一个字符后面带? var reg=/\d?/ /n?/ 代表 ...

  8. kafka创建topics 错误: 找不到或无法加载主类 Files\Java\jdk1.7.0_80\lib;C:\Program

    解决方案如下: 在kafka安装目录中找到bin\windows目录中的kafka-run-class.bat找到%CLASSPATH%为其加上双引号

  9. Python文件的读写

    一.写数据 f = open("hello.txt", "w") f.write("hello world python!") f.clos ...

  10. webpack常见问题 收藏

    一:webpack认识 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地构建一个依赖关 ...