forked from DTStack/Taier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipUtil.java
More file actions
380 lines (349 loc) · 14.1 KB
/
Copy pathZipUtil.java
File metadata and controls
380 lines (349 loc) · 14.1 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dtstack.taier.common.util;
import com.dtstack.taier.common.exception.TaierDefineException;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* @company: www.dtstack.com
* @Author :Nanqi
* @Date :Created in 10:23 2019-07-24
* @Description:Zip 压缩工具
*/
public class ZipUtil {
private static final Logger LOG = LoggerFactory.getLogger(ZipUtil.class);
private static byte[] ZIP_HEADER_1 = new byte[]{80, 75, 3, 4};
private static byte[] ZIP_HEADER_2 = new byte[]{80, 75, 5, 6};
private static byte[] _byte = new byte[1024];
private static final int MAX_ZIP_ENTRY_COUNT = 1000;
private static final int MAX_ZIP_RECURSION_DEPTH = 3;
private static final long MAX_ZIP_TOTAL_UNCOMPRESSED_SIZE = 100L * 1024 * 1024;
private static final long MAX_ZIP_ENTRY_UNCOMPRESSED_SIZE = 50L * 1024 * 1024;
private static final long MAX_ZIP_COMPRESSION_RATIO = 100L;
public static byte[] compress(byte[] rowData) {
byte[] backData = null;
ZipOutputStream zip = null;
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
zip = new ZipOutputStream(bos, StandardCharsets.UTF_8);
ZipEntry entry = new ZipEntry("zip");
entry.setSize(rowData.length);
zip.putNextEntry(entry);
zip.write(rowData);
} catch (Exception ex) {
LOG.error(ex.getMessage(), ex);
} finally {
if (null != zip) {
try {
zip.close();
zip.closeEntry();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
if (null != bos) {
backData = bos.toByteArray();
try {
bos.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
return backData;
}
public static byte[] deCompress(byte[] rowData) {
byte[] backData = null;
ZipInputStream zip = null;
ByteArrayInputStream bis = null;
ByteArrayOutputStream baos = null;
try {
bis = new ByteArrayInputStream(rowData);
zip = new ZipInputStream(bis, StandardCharsets.UTF_8);
while (zip.getNextEntry() != null) {
byte[] buf = new byte[1024];
int num = -1;
baos = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
backData = baos.toByteArray();
baos.flush();
baos.close();
}
} catch (Exception ex) {
LOG.error(ex.getMessage(), ex);
} finally {
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
if (null != zip) {
try {
zip.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
if (null != baos) {
try {
baos.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
return backData;
}
public static String compress(String rowData) {
return new String(Base64Util.baseEncode(compress(rowData.getBytes(StandardCharsets.UTF_8))), StandardCharsets.UTF_8);
}
public static String deCompress(String rowData) {
return new String(deCompress(Base64Util.baseDecode(rowData.getBytes(StandardCharsets.UTF_8))), StandardCharsets.UTF_8);
}
/**
* 压缩文件或路径
*
* @param zip 压缩的目的地址
* @param srcFiles 压缩的源文件
*/
public static void zipFile(String zip, List<File> srcFiles) {
try {
if (zip.endsWith(".zip") || zip.endsWith(".ZIP")) {
org.apache.tools.zip.ZipOutputStream _zipOut = new org.apache.tools.zip.ZipOutputStream(new FileOutputStream(new File(zip)));
_zipOut.setEncoding("GBK");
for (File _f : srcFiles) {
handlerFile(zip, _zipOut, _f, "");
}
_zipOut.close();
} else {
System.out.println("target file[" + zip + "] is not .zip type file");
}
} catch (IOException e) {
}
}
/**
* @param zip 压缩的目的地址
* @param zipOut
* @param srcFile 被压缩的文件信息
* @param path 在zip中的相对路径
* @throws IOException
*/
private static void handlerFile(String zip, org.apache.tools.zip.ZipOutputStream zipOut, File srcFile, String path) throws IOException {
System.out.println(" begin to compression file[" + srcFile.getName() + "]");
if (!"".equals(path) && !path.endsWith(File.separator)) {
path += File.separator;
}
if (!srcFile.getPath().equals(zip)) {
if (srcFile.isDirectory()) {
File[] _files = srcFile.listFiles();
if (_files.length == 0) {
zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(path + srcFile.getName() + File.separator));
zipOut.closeEntry();
} else {
for (File _f : _files) {
handlerFile(zip, zipOut, _f, path + srcFile.getName());
}
}
} else {
try (InputStream _in = new FileInputStream(srcFile)) {
zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(path + srcFile.getName()));
int len = 0;
while ((len = _in.read(_byte)) > 0) {
zipOut.write(_byte, 0, len);
}
} finally {
zipOut.closeEntry();
}
}
}
}
/**
* 解压缩ZIP文件,将ZIP文件里的内容解压到targetDIR目录下
*
* @param zipPath 待解压缩的ZIP文件名
* @param descDir 目标目录
*/
public static List<File> upzipFile(String zipPath, String descDir) {
return upzipFile(new File(zipPath), descDir);
}
/**
* 对.zip文件进行解压缩
*
* @param zipFile 解压缩文件
* @param descDir 压缩的目标地址,如:D:\\测试 或 /mnt/d/测试
* @return
*/
@SuppressWarnings("rawtypes")
public static List<File> upzipFile(File zipFile, String descDir) {
try {
return upzipFile(zipFile, descDir, new UnzipContext(), 0);
} catch (IOException e) {
throw new TaierDefineException(String.format("Unzip exception : %s", e.getMessage()), e);
}
}
@SuppressWarnings("rawtypes")
private static List<File> upzipFile(File zipFile, String descDir, UnzipContext context, int depth) throws IOException {
if (depth > MAX_ZIP_RECURSION_DEPTH) {
throw new IOException(String.format("zip recursion depth exceeds limit: %s", MAX_ZIP_RECURSION_DEPTH));
}
List<File> _list = new ArrayList<>();
File baseDir = new File(descDir);
String basePath = getCanonicalDirPath(baseDir);
ZipFile _zipFile = null;
try {
_zipFile = new ZipFile(zipFile, "GBK");
for (Enumeration entries = _zipFile.getEntries(); entries.hasMoreElements(); ) {
org.apache.tools.zip.ZipEntry entry = (org.apache.tools.zip.ZipEntry) entries.nextElement();
context.addEntry(entry.getName());
File _file = resolveZipEntryFile(baseDir, basePath, entry.getName());
if (_file.isHidden()) {
continue;
}
if (entry.isDirectory()) {
makeDirs(_file);
} else {
File _parent = _file.getParentFile();
makeDirs(_parent);
byte[] buffer = new byte[4];
byte[] fileBuffer = new byte[1024];
int length;
try (InputStream _in = _zipFile.getInputStream(entry);
OutputStream _out = new FileOutputStream(_file)) {
length = _in.read(buffer, 0, 4);
long written = 0L;
if (length > 0) {
_out.write(buffer, 0, length);
written += length;
context.addUncompressedSize(length);
validateEntrySize(entry, written);
}
int len = 0;
while ((len = _in.read(fileBuffer)) > 0) {
_out.write(fileBuffer, 0, len);
written += len;
context.addUncompressedSize(len);
validateEntrySize(entry, written);
}
_out.flush();
}
if (length == 4 && (Arrays.equals(ZIP_HEADER_1, buffer) || Arrays.equals(ZIP_HEADER_2, buffer))) {
_list.addAll(upzipFile(_file, _file.getPath() + "tmp", context, depth + 1));
} else {
_list.add(_file);
}
}
}
} finally {
if (_zipFile != null) {
_zipFile.close();
}
}
return _list;
}
private static File resolveZipEntryFile(File baseDir, String basePath, String entryName) throws IOException {
File targetFile = new File(baseDir, entryName);
String targetPath = targetFile.getCanonicalPath();
if (!targetPath.equals(basePath) && !targetPath.startsWith(basePath + File.separator)) {
throw new IOException(String.format("zip entry is outside of target dir: %s", entryName));
}
return targetFile;
}
private static String getCanonicalDirPath(File dir) throws IOException {
makeDirs(dir);
return dir.getCanonicalPath();
}
private static void makeDirs(File dir) throws IOException {
if (dir != null && !dir.exists() && !dir.mkdirs()) {
throw new IOException(String.format("failed to create directory: %s", dir));
}
}
private static void validateEntrySize(org.apache.tools.zip.ZipEntry entry, long written) throws IOException {
if (written > MAX_ZIP_ENTRY_UNCOMPRESSED_SIZE) {
throw new IOException(String.format("zip entry size exceeds limit: %s", entry.getName()));
}
long compressedSize = entry.getCompressedSize();
if (compressedSize > 0 && written > compressedSize * MAX_ZIP_COMPRESSION_RATIO) {
throw new IOException(String.format("zip entry compression ratio exceeds limit: %s", entry.getName()));
}
}
private static class UnzipContext {
private int entryCount;
private long totalUncompressedSize;
private void addEntry(String entryName) throws IOException {
entryCount++;
if (entryCount > MAX_ZIP_ENTRY_COUNT) {
throw new IOException(String.format("zip entry count exceeds limit: %s", entryName));
}
}
private void addUncompressedSize(long size) throws IOException {
totalUncompressedSize += size;
if (totalUncompressedSize > MAX_ZIP_TOTAL_UNCOMPRESSED_SIZE) {
throw new IOException("zip total uncompressed size exceeds limit");
}
}
}
/**
* 对临时生成的文件夹和文件夹下的文件进行删除
*/
public static void deletefile(String delpath) {
try {
File file = new File(delpath);
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
if (null == filelist) {
return;
}
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + File.separator + filelist[i]);
if (!delfile.isDirectory()) {
delfile.delete();
} else if (delfile.isDirectory()) {
deletefile(delpath + File.separator + filelist[i]);
}
}
file.delete();
}
} catch (Exception e) {
LOG.error("delete path " + delpath, e);
}
}
}