Java Zip

概述

由于最近毕业设计的需要,需要使用到Zip的解压、压缩功能,还要有能加密解密,所以去找了相关的Java zip的包,封装了下。

需要

Java Zip 解压、压缩的功能,需要用到zip4j这个jar文件,当然,我使用maven直接下载的。

pom.xml里直接添加如下代码就好了:

1
2
3
4
5
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>

代码

好了,直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.orleven.tentacle.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
/**
* Zip 处理工具包
* 需要注意的是如果压缩包里已经存在要添加的文件,会陷入死循环,所以添加之前要先判断一下。
* @author orleven
* @date 2017年3月8日
*/
public class ZipUtil {
/**
* 添加文件夹到zip中
* @param inPath
* @param outPath
* @param password
* @return
*/
public static boolean addFoldInZip(String inPath,String storagePath,String outPath,String password) {
try {
ZipFile zipFile = new ZipFile(outPath);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setRootFolderInZip(storagePath); ;
if(password!=null&&!password.equals("")){
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(password);
}
zipFile.addFolder(inPath, parameters);
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
/**
* 添加文件到zip中指定的文件夹中
* @param inPath
* @param storagePath
* @param outPath
* @param password
* @return
*/
public static boolean addFileInZip(String inPath,String storagePath,String outPath,String password) {
try {
ZipFile zipFile = new ZipFile(outPath);
File inFile = new File(inPath);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setRootFolderInZip(storagePath);
if(password!=null&&!password.equals("")){
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(password);
}
zipFile.addFile(inFile, parameters);
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
/**
* 添加多个文件到zip中指定的文件夹中
* @param inPath
* @param storagePath
* @param outPath
* @param password
* @return
*/
public static boolean addFilesInZip(ArrayList<File> inFiles,String storagePath,String outPath,String password) {
try {
ArrayList filesToAdd = new ArrayList();
ZipFile zipFile = new ZipFile(outPath);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setRootFolderInZip(storagePath);
if(password!=null&&!password.equals("")){
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(password);
}
zipFile.addFiles(inFiles, parameters);
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
/**
* 从zip中删除文件
* @param inPath
* @param outPath
* @param password
* @return
*/
public static boolean removeFileInZip(String inPath,String storagePath,String password) {
try {
ZipFile zipFile = new ZipFile(inPath);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
List fileHeaderList = zipFile.getFileHeaders();
storagePath = storagePath.replaceAll("\\\\", "/");
for (int i =fileHeaderList.size() -1; i>0 ; i--) {
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
if(fileHeader.getFileName().indexOf(storagePath)==0){
System.out.println("Name: " + fileHeader.getFileName());
zipFile.removeFile(fileHeader.getFileName());
}
}
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
/**
* 查看压缩包的文件列表
* @param inPath
* @param password
* @return
*/
public static boolean getNameFromZip(String inPath,String password) {
try {
ZipFile zipFile = new ZipFile(inPath);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
List fileHeaderList = zipFile.getFileHeaders();
for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
System.out.println("Name: " + fileHeader.getFileName());
System.out.println("Compressed Size: " + fileHeader.getCompressedSize());
System.out.println("Uncompressed Size: " + fileHeader.getUncompressedSize());
System.out.println("CRC: " + fileHeader.getCrc32());
System.out.println("************************************************************");
}
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
/**
* 解压zip里的所有文件
* @param inPath
* @param outPath
* @param password
* @return
*/
public static boolean extractZip(String inPath,String outPath ,String password) {
try {
ZipFile zipFile = new ZipFile(inPath);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
zipFile.extractAll(outPath);
System.out.println(password);
return true;
} catch (ZipException e) {
// e.printStackTrace();
return false;
}
}
/**
* 解压zip里的文件
* @param inPath
* @param storagePath
* @param outPath
* @param password
* @return
*/
public static boolean extractFileFromZip(String inPath,String storagePath,String outPath ,String password) {
try {
ZipFile zipFile = new ZipFile(inPath);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
List fileHeaderList = zipFile.getFileHeaders();
storagePath = storagePath.replaceAll("\\\\", "/");
for (int i =0;i<fileHeaderList.size() ;i++) {
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
if(fileHeader.getFileName().indexOf(storagePath)==0){
zipFile.extractFile(fileHeader, outPath);
zipFile.removeFile(fileHeader.getFileName());
}
}
return true;
} catch (ZipException e) {
e.printStackTrace();
return false;
}
}
/**
* 测试zip 压缩算法
*/
public static void main(String[] args) {
System.out.println("Zip压缩/解压缩测试");
// System.out.println("添加文件夹压缩文件");
// String inPath = "C:\\Users\\dell\\Desktop\\测试文件";
// String outPath = "C:\\Users\\dell\\Desktop\\test.zip";
// String storagePath = null;
// String password = "123456";
// ZipUtil.addFoldInZip(inPath, storagePath,outPath,password);
// System.out.println("添加文件到压缩文件");
// String inPath1 = "C:\\Users\\dell\\Desktop\\IEEE_TPDS2003.docx";
// String outPath1 = "C:\\Users\\dell\\Desktop\\test.zip";
// String storagePath1 = null;
// String password1 = "";
// ZipUtil.addFileInZip(inPath1, storagePath1,outPath1,password1);
// System.out.println("删除zip的某个文件");
// String storagePath2 = "测试文件\\毕业论文模板201435";
// String inPath2 = "C:\\Users\\dell\\Desktop\\test.zip";
// String password2 = "123456";
// ZipUtil.removeFileInZip(inPath2, storagePath2, password2);
// System.out.println("查看zip文件中的内容");
// String inPath3 = "C:\\Users\\dell\\Desktop\\test.zip";
// String password3 = "123456";
// ZipUtil.getNameFromZip(inPath3,password3);
// System.out.println("解压压缩文件");
// String inPath4 = "C:\\Users\\dell\\Desktop\\www.zip";
// String outPath4 = "C:\\Users\\dell\\Desktop\\";
// String password4 = "123456";
// ZipUtil.extractZip(inPath4, outPath4, password4);
// System.out.println("解压zip中的某个文件");
// String inPath5 = "C:\\Users\\dell\\Desktop\\test.zip";
// String outPath5 = "C:\\Users\\dell\\Desktop\\";
// String password5 = "123456";
// String storagePath5 = "测试文件\\毕业论文模板201435";
// ZipUtil.extractFileFromZip(inPath5, storagePath5, outPath5, password5);
System.out.println("已经输出!");
}
}