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 ...
随机推荐
- linux rpm 命令
# 产看软件包信息[root@devops201 k8s_install]# rpm -qi yum-utilsName : yum-utilsVersion : 1.1.31Release : 54 ...
- React.CreateContext
跨组件传递的内容组件,该组件导出两个对象Provider 提供数据, Consumer, 消费数据 Context被翻译为上下文,在React的官方文档中归类于高级部分,属于React的高级API,但 ...
- Python基础前言
计算机内部存储数据的原理 """计算机内部只认识01二进制"""是因为计算机是基于电工作的,而电是有高低电频之分00000001 000 ...
- 【坑】 centos 7.6 使用ssh 秘钥登录遇到的问题
问题: 我在master1上面创建了一个ssh的秘钥文件,通过ssh-copy-id 传送四台主机,然后使用ansible all -m ping 发现有台机器就是有问题 这是怎么回事???一样的配置 ...
- oracle abs ap 银行账号 组织层 新增api
DECLARE l_return_status VARCHAR2(1); l_msg_count NUMBER; l_msg_data VARCHAR2(1000); x_acct_use_id nu ...
- Xmind 8 pro 破解版详细安装教程
写在前面的废话: 本来网上就有很多破解版的Xmind,也有安装教程,为什么我还要多此一举做这个教程呢? 因为实际安装后,使用起来总会有各种小瑕疵,最烦人的问题就是,xmind文件和软件无法关联,双击x ...
- DOS批处理命令,自动获取本机系统及硬件配置信息
可以配合域策略自动下发执行, 批量收集域内电脑配置; 手动执行亦可; 如下保存成.bat批处理文件执行即可. /*&cls&echo off&cd /d "%~dp0 ...
- 压缩打包,tar
压缩打包命令 创建压缩包 tar -zcvf /home/xxxx.tar.gz /xahot 解压 tar -zxvf xxx.tar.gz gzip xxxx 自动变为 xxx.gz
- 关系型数据库,基表Guid 主键设值
在我们开发过程,为了自动适应新增修改,可以对基表,Guid 类型进行如下设置: public bool IsTransient() { return this.Id == Guid.Emp ...
- Comparison Theorem in Riemannian geometry
Given $p\in M$, locally, there exists a diffemorphism of $T_M$ and $B_r(p)\subset$, this is the most ...