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
|
package org.openecomp.core.tools.util;
import com.google.common.io.ByteStreams;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);
public static void createZip(String zipFileName, Path dir) throws IOException {
File dirObj = dir.toFile();
Path zippedFile = Files.createFile(Paths.get(zipFileName));
try (
FileOutputStream fileOutputStream = new FileOutputStream(File.separator + zippedFile.toFile());
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
ZipOutputStream out = new ZipOutputStream(bos)) {
File[] files = dirObj.listFiles();
for (File file : files) {
out.putNextEntry(new ZipEntry(file.getName()));
Files.copy(Paths.get(file.getPath()), out);
out.closeEntry();
}
Utils.printMessage(logger, "Zip file was created " + zipFileName);
}
}
public static final Set<String> cleanStr(Set<String> inFilterStrs) {
return inFilterStrs.stream().map(inFilterStr -> {
if (Objects.isNull(inFilterStr)) {
return inFilterStr;
}
Scanner scan = new Scanner(inFilterStr);
while (scan.hasNextLine()) {
inFilterStr = scan.nextLine().replaceAll("[^a-zA-Z0-9]", "");
}
return inFilterStr;
}
).collect(Collectors.toSet());
}
static void addDir(File dirObj, ZipOutputStream out, String root, Set<String> filterItem) throws IOException {
File[] files = dirObj.listFiles();
filterItem = cleanStr(filterItem);
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
addDir(files[i], out, root, filterItem);
String filePath = files[i].getAbsolutePath().replace(root + File.separator, "") + File.separator;
out.putNextEntry(new ZipEntry(filePath));
continue;
}
try (FileInputStream in = new FileInputStream((files[i].getAbsolutePath()))) {
String filePath = files[i].getAbsolutePath().replace(root + File.separator, "");
if (filterItem.isEmpty() || filterItem.stream().anyMatch(s -> filePath.contains(s))) {
out.putNextEntry(new ZipEntry(filePath));
try {
ByteStreams.copy(in, out);
} finally {
out.closeEntry();
}
}
}
}
}
public static void unzip(Path zipFile, Path outputFolder) throws IOException {
if (zipFile == null || outputFolder == null) {
return;
}
if (!outputFolder.toFile().exists()) {
Files.createDirectories(outputFolder);
}
try (FileInputStream fileInputStream = new FileInputStream(zipFile.toFile());
ZipInputStream zis = new ZipInputStream(fileInputStream)) {
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(outputFolder.toString() + File.separator + fileName);
if (ze.isDirectory()) {
Path path = newFile.toPath();
if (!path.toFile().exists()) {
Files.createDirectories(path);
}
} else {
new File(newFile.getParent()).mkdirs();
try (FileOutputStream fos = new FileOutputStream(newFile)) {
ByteStreams.copy(zis, fos);
}
}
ze = zis.getNextEntry();
}
zis.closeEntry();
}
}
}
|