2f6693d133f78c710c10e093acdbd49310fdb4c7
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / util / ZipUtils.java
1 package org.openecomp.core.tools.util;
2
3 import com.google.common.io.ByteStreams;
4
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.nio.file.Files;
10 import java.nio.file.Path;
11 import java.util.Objects;
12 import java.util.Scanner;
13 import java.util.Set;
14 import java.util.stream.Collectors;
15 import java.util.zip.ZipEntry;
16 import java.util.zip.ZipInputStream;
17 import java.util.zip.ZipOutputStream;
18
19 public class ZipUtils {
20     public static void createZip(String zipFileName, Path dir, Set<String> filterItem) throws Exception {
21         File dirObj = dir.toFile();
22         try (
23                 FileOutputStream fileOutputStream = new FileOutputStream(zipFileName);
24                 ZipOutputStream out = new ZipOutputStream(fileOutputStream)) {
25             addDir(dirObj, out, dir.toString(), filterItem);
26         }
27     }
28
29     public static final Set<String> cleanStr(Set<String> inFilterStrs) {
30         return inFilterStrs.stream().map(inFilterStr -> {
31                     if (Objects.isNull(inFilterStr)) {
32                         return inFilterStr;
33                     }
34                     Scanner scan = new Scanner(inFilterStr);
35                     while (scan.hasNextLine()) {
36                         inFilterStr = scan.nextLine().replaceAll("[^a-zA-Z0-9]", "");
37                     }
38                     return inFilterStr;
39                 }
40         ).collect(Collectors.toSet());
41     }
42
43     static void addDir(File dirObj, ZipOutputStream out, String root, Set<String> filterItem) throws IOException {
44         File[] files = dirObj.listFiles();
45         filterItem = cleanStr(filterItem);
46
47         for (int i = 0; i < files.length; i++) {
48             if (files[i].isDirectory()) {
49                 addDir(files[i], out, root, filterItem);
50                 String filePath = files[i].getAbsolutePath().replace(root + File.separator, "") + "/";
51                 out.putNextEntry(new ZipEntry(filePath));
52                 continue;
53             }
54             try (FileInputStream in = new FileInputStream((files[i].getAbsolutePath()))) {
55                 String filePath = files[i].getAbsolutePath().replace(root + File.separator, "");
56                 if (filterItem.isEmpty() || filterItem.stream().anyMatch(s -> filePath.contains(s))) {
57                     out.putNextEntry(new ZipEntry(filePath));
58                     try {
59                         ByteStreams.copy(in, out);
60
61                     } finally {
62                         out.closeEntry();
63                     }
64                 }
65
66             }
67         }
68     }
69
70     public static void unzip(Path zipFile, Path outputFolder) throws IOException {
71         if (zipFile == null || outputFolder == null) {
72             return;
73         }
74         if (!Files.exists(outputFolder)) {
75             Files.createDirectories(outputFolder);
76         }
77
78         try (FileInputStream fileInputStream = new FileInputStream(zipFile.toFile());
79              ZipInputStream zis = new ZipInputStream(fileInputStream)) {
80             ZipEntry ze = zis.getNextEntry();
81             while (ze != null) {
82                 String fileName = ze.getName();
83                 File newFile = new File(outputFolder.toString() + File.separator + fileName);
84                 if (ze.isDirectory()) {
85                     Path path = newFile.toPath();
86                     if (!Files.exists(path)) {
87                         Files.createDirectories(path);
88                     }
89                 } else {
90                     new File(newFile.getParent()).mkdirs();
91                     try (FileOutputStream fos = new FileOutputStream(newFile)) {
92                         ByteStreams.copy(zis, fos);
93                     }
94                 }
95                 ze = zis.getNextEntry();
96             }
97
98             zis.closeEntry();
99         }
100
101     }
102 }
103