ZIP文件操作工具类
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.commons.io.FilenameUtils;
5
6 import java.io.*;
7 import java.nio.charset.Charset;
8 import java.util.zip.ZipEntry;
9 import java.util.zip.ZipInputStream;
10 import java.util.zip.ZipOutputStream;
11
12
13 @Slf4j
14 public class PfZipUtil {
15 public static final int BUFFER_SIZE = 2048;
16
17 private PfZipUtil() {
18 }
19
20 public static void zipFile(String toZipFilePath, String zipFilePath) throws IOException {
21 InputStream inputStream = null;
22 ZipOutputStream zipOutputStream = null;
23 File toZipFile = new File(toZipFilePath);
24 File zipFile = new File(zipFilePath);
25 try {
26 inputStream = new FileInputStream(toZipFile);
27 zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
28 zipOutputStream.putNextEntry(new ZipEntry(toZipFile.getName()));
29 int currentByte = -1;
30 log.debug(String.valueOf(currentByte));
31 byte[] data = new byte[BUFFER_SIZE];
32 while ((currentByte = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {
33 zipOutputStream.write(data, 0, currentByte);
34 }
35 zipOutputStream.flush();
36 } finally {
37 SafeCloseUtil.safeClose(inputStream);
38 SafeCloseUtil.safeClose(zipOutputStream);
39 if (toZipFile.exists()) {
40 boolean isDeleted = toZipFile.delete();
41 if (!isDeleted) {
42 log.warn("delete file failed " + toZipFile.getAbsolutePath());
43 }
44 }
45 }
46 }
47
48 public static void zipFiles(File[] toZipFilePath, String zipFilePath) throws IOException {
49 InputStream inputStream = null;
50 zipFilePath = PfStringUtil.getLegalPath(zipFilePath);
51 File zipFile = new File(zipFilePath);
52 FileOutputStream outputStream = new FileOutputStream(zipFile);
53 ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
54 try {
55 for (File file : toZipFilePath) {
56 inputStream = new FileInputStream(file.getAbsolutePath());
57 zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
58 int currentByte = -1;
59 log.debug(String.valueOf(currentByte));
60 byte[] data = new byte[BUFFER_SIZE];
61 while ((currentByte = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {
62 zipOutputStream.write(data, 0, currentByte);
63 }
64 zipOutputStream.closeEntry();
65 inputStream.close();
66 }
67 zipOutputStream.close();
68 } finally {
69 SafeCloseUtil.safeClose(inputStream);
70 SafeCloseUtil.safeClose(zipOutputStream);
71 for (File file : toZipFilePath) {
72 if (file.exists()) {
73 boolean isDeleted = file.delete();
74 if (!isDeleted) {
75 log.warn("delete file failed " + file.getAbsolutePath());
76 }
77 }
78 }
79 }
80 }
81
82 public static void createZip(String sourcePath, String zipPathNo) {
83 String zipPath = FileUtil.verifyNonNullString(zipPathNo);
84 if (zipPath == null) {
85 return;
86 }
87 FileOutputStream fos = null;
88 ZipOutputStream zos = null;
89 try {
90 fos = new FileOutputStream(zipPath);
91 zos = new ZipOutputStream(fos);
92 String sourcePath2 = FileUtil.verifyNonNullString(sourcePath);
93 if (sourcePath2 != null) {
94 writeZip(new File(sourcePath2), "", zos);
95 }
96 } catch (FileNotFoundException e) {
97 log.error("creat zipfile error", e);
98 } finally {
99 try {
100 if (zos != null) {
101 zos.close();
102 }
103 if (fos != null) {
104 fos.close();
105 }
106 } catch (IOException e) {
107 log.error("creat zipfile error", e);
108 }
109
110 }
111 }
112
113 private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
114 if (file.exists()) {
115 if (file.isDirectory()) {// 处理文件夹
116 parentPath += file.getName() + File.separator;
117 File[] files = file.listFiles();
118 if (files != null && files.length != 0) {
119 for (File f : files) {
120 writeZip(f, parentPath, zos);
121 }
122 } else { // 空目录则创建当前目录
123 try {
124 zos.putNextEntry(new ZipEntry(parentPath));
125 } catch (IOException e) {
126 e.printStackTrace();
127 }
128 }
129 } else {
130 FileInputStream fis = null;
131 try {
132 fis = new FileInputStream(file);
133 ZipEntry ze = new ZipEntry(parentPath + file.getName());
134 zos.putNextEntry(ze);
135 byte[] content = new byte[1024];
136 int len = -1;
137 log.debug(String.valueOf(len));
138 while ((len = fis.read(content)) != -1) {
139 zos.write(content, 0, len);
140 zos.flush();
141 }
142
143 } catch (FileNotFoundException e) {
144 log.error("creat zipfile error", e);
145 } catch (IOException e) {
146 log.error("creat zipfile error", e);
147 } finally {
148 try {
149 if (fis != null) {
150 fis.close();
151 }
152 } catch (IOException e) {
153 log.error("creat zipfile error", e);
154 }
155 }
156 }
157 }
158 }
159
160 private static void createDirIfNotExists(String destPath, String entryName) {
161 String destPath2 = FileUtil.verifyNonNullString(destPath + File.separator + entryName);
162 if (destPath2 != null) {
163 File file = new File(destPath2);
164 if (!file.exists()) {
165 boolean success = file.mkdirs();
166 if (!success) {
167 log.info("mkdir error,path:" + file);
168 }
169 }
170 }
171 }
172
173 /**
174 * @param zipFileName
175 * @param destPath
176 * @return 解压文件的根目录
177 */
178 public static File unZip(String zipFileName, String destPath) {
179
180 zipFileName = FileUtil.verifyNonNullString(zipFileName);
181 destPath = FileUtil.verifyNonNullString(destPath);
182 if (zipFileName == null || zipFileName.length() <= 0) {
183 log.warn("zipFileName is empty!");
184 return null;
185 }
186 if (destPath == null || destPath.length() <= 0) {
187 log.warn("destPath is empty!");
188 return null;
189 }
190
191 FileInputStream fin = null;
192 ZipInputStream in = null;
193 FileOutputStream os = null;
194 try {
195 String zipFileName1 = FileUtil.verifyNonNullString(zipFileName);
196 fin = new FileInputStream(zipFileName1);
197 in = new ZipInputStream(fin);
198 // ZIP 文件条目
199 ZipEntry entry;
200
201 while ((entry = in.getNextEntry()) != null) {
202 String entryName = entry.getName();
203 // 判断是否为目录条目,目录条目定义为其名称以 '/'
204 // 结尾的条目。
205 entryName = FilenameUtils.normalize(entryName);
206 if (entry.isDirectory()) {
207 createDirIfNotExists(destPath, entryName);
208 } else {
209 // 如果压缩包存在路径conf/explain.xml,存在conf文件夹,但java读取不到conf文件夹,而是直接读取其中的文件。我们要先判断conf文件夹是否已经
210 // 创建,如果没有必须先建立conf,否则解压失败
211 String path = destPath + File.separator + entryName;
212 /**
213 * 下面两行代码是为了屏蔽掉路径中的"/",统一为"\",
214 */
215 String path2 = FileUtil.verifyNonNullString(path);
216 if (path2 != null) {
217 File file = new File(path2);
218 String tempPath = file.getPath();
219
220 tempPath = tempPath.substring(0, tempPath.lastIndexOf(File.separator));
221 String tempPath2 = FileUtil.verifyNonNullString(tempPath);
222 if (tempPath2 != null) {
223 File tempFile = new File(tempPath2);
224 if (!tempFile.exists()) {
225 boolean success = tempFile.mkdirs();
226 if (!success) {
227 log.info("mkdir error,path:");
228 }
229 }
230 }
231 }
232 String destPath2 = FileUtil.verifyNonNullString(destPath + File.separator + entryName);
233 if (destPath2 != null) {
234 os = new FileOutputStream(destPath2);
235 // Transfer bytes from the ZIP
236 // file to the output file
237 byte[] buf = new byte[1024];
238 int len = 0;
239 log.debug(String.valueOf(len));
240 while ((len = in.read(buf)) > 0) {
241 os.write(buf, 0, len);
242 }
243 os.close();
244 in.closeEntry();
245 }
246 //fin.close();
247 }
248 }
249 return new File(destPath);
250 } catch (IOException e) {
251 log.error(e.getMessage(), e);
252 } finally {
253 closeFileResource(fin, in, os);
254 }
255 return null;
256 }
257
258 /**
259 * 计算ZIP文件的解压大小
260 *
261 * @param inputStream 输入流
262 * 注:输入流由调用方负责关闭
263 * @return 解压大小
264 */
265 public static long getUncompressedSize(InputStream inputStream) throws IOException {
266 ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("GBK"));
267 byte[] buffer = new byte[8 * 1024 * 1024];
268 long uncompressedSize = 0;
269 ZipEntry zipEntry;
270 while ((zipEntry = zipInputStream.getNextEntry()) != null) {
271 long size = zipEntry.getSize();
272 // ZipEntry的size可能为-1,表示未知
273 if (size == -1) {
274 int len;
275 while ((len = zipInputStream.read(buffer, 0, buffer.length)) != -1) {
276 uncompressedSize += len;
277 }
278 } else {
279 uncompressedSize += size;
280 }
281 zipInputStream.closeEntry();
282 }
283 return uncompressedSize;
284 }
285
286 private static void closeFileResource(FileInputStream fin, ZipInputStream in, FileOutputStream os) {
287 if (fin != null) {
288 try {
289 fin.close();
290 } catch (IOException e) {
291 log.error(e.getMessage(), e);
292 }
293
294 }
295 if (in != null) {
296 try {
297 in.close();
298 } catch (IOException e) {
299 log.error(e.getMessage(), e);
300 }
301
302 }
303 if (os != null) {
304 try {
305 os.close();
306 } catch (IOException e) {
307 log.error(e.getMessage(), e);
308 }
309
310 }
311 }
312
313 }
ZIP文件操作工具类的更多相关文章
- 文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.
FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...
- Code片段 : .properties属性文件操作工具类 & JSON工具类
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...
- JAVA文件操作工具类(读、增、删除、复制)
使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...
- Android文件操作工具类(转)
Android文件操作工具类(转) 2014/4/3 18:13:35 孤独的旅行家 博客园 这个工具类包含Android应用开发最基本的几个文件操作方法,也是我第一次发博客与大家分享自己写的东 ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(4)-文件操作工具类FileOperationHelper
文件操作是非常通用的,注释都写在源代码中了,不多说~需要特别说明的是,任务的异步执行和IOperationProgressListener.拷贝和删除等操作,是比较费时的,采用了异步执行的方式~ An ...
- Java文件操作工具类(复制、删除、重命名、创建路径)
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- C#文件操作工具类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- Java文件操作工具类
import com.foriseland.fjf.lang.DateUtil;import org.apache.commons.io.FileUtils;import org.slf4j.Logg ...
- Java IO(文件操作工具类)
FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指 ...
- 文件操作工具类FileUtils
package yqw.java.util; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import ...
随机推荐
- CVE-2013-2566 SSL/TLS RC4 信息泄露漏洞 修复方案
详细描述 安全套接层(Secure Sockets Layer,SSL),一种安全协议,是网景公司(Netscape)在推出Web浏览器首版的同时提出的,目的是为网络通信提供安全及数据完整性.SSL在 ...
- Lua中创建新的文件夹
如下: os.execute('mkdir 文件夹名称')
- 【Go】发送请求
发送post请求 reqMap := make(map[string]interface{}) reqMap["order_num"] = request.OutTradeNo r ...
- 关于certutil的探究-文件下载+编码分块上传上传文件再合并
何为certutil certutil.exe 是一个合法Windows文件,用于管理Windows证书的程序. 微软官方是这样对它解释的: Certutil.exe是一个命令行程序,作为证书服务的一 ...
- TM1621断码液晶驱动IC的原理、驱动代码
TM1621是一个多功能的LCD驱动器,带有蜂鸣器驱动功能.通讯采用四线串行接口 TM1621的难点在于字节序和显存跟屏幕的映射关系上,下面是写寄存器的代码 void Delay_us(uint8_t ...
- python 爬虫 selenium 与 chromedriver
selenium 安装 pip install selenium chromedriver 下载 https://npm.taobao.org/mirrors/chromedriver?spm ...
- 如何理解Vue中的组件?
Vue2.6已经更新了关于内容插槽和作用域插槽的API和用法,为了不误导大家,我把插槽的内容删除了.详情请看官网 2018-07-19更新: 更新作用域插槽的属性: scope -> slot- ...
- 小白之Python-基础中的基础01
Python-基础中的基础01 第一次写博客笔记,尝试并监督下自己. 每一天都值得期待! 20170803 -----------------华丽的分界线------------- Python之-- ...
- XenForo论坛安装
1.下载安装程序,程序可以到qq群里面找,或者是联系我 2.域名+/install安装 3.汉化后台,访问https://www.cnxfans.com/resources/xenforo-2-x.1 ...
- Neural Network模型复杂度之Dropout - Python实现
背景介绍 Neural Network之模型复杂度主要取决于优化参数个数与参数变化范围. 优化参数个数可手动调节, 参数变化范围可通过正则化技术加以限制. 本文从优化参数个数出发, 以dropout技 ...