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文件操作工具类的更多相关文章

  1. 文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.

    FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...

  2. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

  3. JAVA文件操作工具类(读、增、删除、复制)

    使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...

  4. Android文件操作工具类(转)

    Android文件操作工具类(转)  2014/4/3 18:13:35  孤独的旅行家  博客园 这个工具类包含Android应用开发最基本的几个文件操作方法,也是我第一次发博客与大家分享自己写的东 ...

  5. 小米开源文件管理器MiCodeFileExplorer-源码研究(4)-文件操作工具类FileOperationHelper

    文件操作是非常通用的,注释都写在源代码中了,不多说~需要特别说明的是,任务的异步执行和IOperationProgressListener.拷贝和删除等操作,是比较费时的,采用了异步执行的方式~ An ...

  6. Java文件操作工具类(复制、删除、重命名、创建路径)

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  7. C#文件操作工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. Java文件操作工具类

    import com.foriseland.fjf.lang.DateUtil;import org.apache.commons.io.FileUtils;import org.slf4j.Logg ...

  9. Java IO(文件操作工具类)

    FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指 ...

  10. 文件操作工具类FileUtils

    package yqw.java.util; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import ...

随机推荐

  1. springboot中实现逆向工程

    如果这篇文章能给你带来帮助 不胜荣幸,如果有不同的意见也欢迎批评指正,废话不多说直接上代码.(参考文档:https://www.cnblogs.com/kibana/p/8930248.html) 第 ...

  2. VMvare虚拟机的安装及新建虚拟机(一)

    a:hover { color: rgba(255, 102, 0, 1) } 一.VMvare虚拟机的安装 1.首先双击--你下载的安装包,这里我分享百度云盘,供大家下载:http://pan.ba ...

  3. 02 python初识

    Python初识 一.入门基础 1. 第一个Python程序 python 代码都是编写在以 .py 结尾的文件中.我们随便新建一个文件,并将文件后缀名改为 .py,在里面编写我们的第一个 pytho ...

  4. [Err] [Dtf] 1044 - Access denied for user 'root'@'localhost' to database 'information_schema'

    在从Oracle向mysql数据库传输数据时,报出来这个错误,原因是因为没有打开mysql数据库,在navicat里打开mysql,并选中要传输的数据库 再重复传输一下即可

  5. 使用Libusb和hidapi测试HID设备

    一.测试中断或者Bulk传输: 首先要使用Libusb打印出HID设备的Endpoint查看是否支持中断或者Bulk传输模式:如果支持的话才可以进一步测试: 因为HID设备在插入的时候无需安装,并且一 ...

  6. Delphi 移除窗口最大化按钮

    很遗憾,好像没有直接的代码可以操作,可以试试以下代码: var GWL_Result: Integer; begin GWL_Result:= GetWindowLong(Handle,GWL_STY ...

  7. Android GNSS模块详解

    1. 参考https://blog.csdn.net/yang_mao_shan/category_12133410.html GNSS 架构是从 应用层 ---> 通过原生 jar 包 --- ...

  8. Kubernetes-yaml详解

    目录: Yaml语法格式 查看api资源版本标签 deployment模板 service模板 查询帮助和格式指令 Pod模板 写 yaml太累怎么办 yaml文件的学习 方法 deployment. ...

  9. verilog 和system verilog 文件操作

    1. 文件操作 Verilog具有系统任务和功能,可以打开文件.将值输出到文件.从文件中读取值并加 载到其他变量和关闭文件. 1.1 Verilog文件操作 1.1.1 打开和关闭文件 module ...

  10. fsck.fat 检查修复(MS-DOS)fat类型文件系统

    使用方式 fsck.fat [option] DEVICE 例如 fsck.fat -aw /dev/usba0 fsck.fat 检查fat文件系统的一致性,并选择性的尝试修复他们. 如下文件系统问 ...