UnzipUtil
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的更多相关文章
- JAVA利用Zip4j解压缩【转】
官方地址:http://www.lingala.net/zip4j/(需要FQ) jar包:http://pan.baidu.com/s/145hwI 演示包:http://pan.baidu.com ...
- java解压缩zip
依赖的包: <!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> <dependency> <gr ...
- java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
// java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...
随机推荐
- C#-导入Excel 内容到 DataTable中
C#-导入Excel 内容到 DataTable中 直接传入文件路径,支持所有Excel格式. 缺点:如果数据量庞大会很占内存. public static DataTable ImportExcel ...
- 微信小程序免费Https获取以及Ubuntu Nginx配置
先贴上Nginx的配置文件 user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections ...
- JavaScript(一)
JavaScript介绍 JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互. JavaScript是浏览器解释执行的,前端脚 ...
- 最近公共祖先问题(LCA)的几种实现方式
LCA也是很经典的内容了,我这个蒟蒻居然今天才开始弄QAQ 我太弱啦! 照例先上定义——————转自维基百科 在图论和计算机科学中,最近公共祖先是指在一个树或者有向无环图中同时拥有v和w作为后代的最深 ...
- GMA Round 1 新年的复数
传送门 新年的复数 已知$\left\{\begin{matrix}A>B>0\\ AB=1\\ (A+B)(A-B)=2\sqrt{3}\end{matrix}\right.$ 求$(A ...
- Linux安装nginx详细步骤
安装依赖 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 在/usr/local/下创建一个nginx的文件夹 ...
- Jumpserver之设置开机自启动
vi /usr/lib/systemd/system/jms.service [Unit] Description=jms After=network.target mariadb.service r ...
- 这样,可以在firefox播放flash了
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="62" heigh ...
- easyui技术积累
1.easyui-textbox与easyui-combobox的onChange事件 //在页面载入后执行下面函数$('#xxx_id').textbox({ onChange: function( ...
- en-zh(科学技术)science and technology-2
研究:长期不吃早餐,患心脏病风险增加87% Skipping breakfast could raise risk of heart disease by 87% Skipping breakfast ...