1 package org.openecomp.portalapp.widget.utils;
3 import java.io.BufferedOutputStream;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.nio.file.Files;
9 import java.nio.file.Paths;
10 import java.util.HashMap;
12 import java.util.Stack;
13 import java.util.zip.ZipEntry;
14 import java.util.zip.ZipInputStream;
16 import org.openecomp.portalapp.widget.constant.WidgetConstant;
17 import org.openecomp.portalapp.widget.service.impl.StorageServiceImpl;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 public class UnzipUtil {
23 * Size of the buffer to read/write data
25 private static final int BUFFER_SIZE = 4096;
26 private static final Logger logger = LoggerFactory.getLogger(UnzipUtil.class);
28 * Extracts a zip file specified by the zipFilePath to a directory specified by
29 * destDirectory (will be created if does not exists)
31 * @param destDirectory
35 public Map<String, byte[]> unzip_db(String zipFilePath, String destDirectory, String widgetName) throws IOException {
37 logger.debug("UnzipUtil.unzip_db: unzip widget file {}", widgetName);
38 File destDir = new File(destDirectory);
39 if (!destDir.exists()) {
42 ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
43 ZipEntry entry = zipIn.getNextEntry();
44 Map<String, byte[]> map = new HashMap<>();
46 map.put(WidgetConstant.WIDGET_CONTROLLER_LOCATION, null);
47 map.put(WidgetConstant.WIDGET_MARKUP_LOCATION, null);
48 map.put(WidgetConstant.WIDGET_STYLE_LOCATION, null);
51 Stack<File> stack = new Stack<>();
53 // iterates over entries in the zip file
54 while (entry != null) {
55 String filePath = destDirectory + File.separator + widgetName + File.separator +
56 entry.getName().substring(entry.getName().indexOf("/")+1);
57 logger.debug("UnzipUtil.unzip_db: file path " + filePath);
58 if (!entry.isDirectory()) {
59 // if the entry is a file, extracts it
60 logger.debug("UnzipUtil.unzip_db: unzip and save widget file {}", filePath);
61 stack.push(new File(filePath));
62 extractFile(zipIn, filePath);
64 // if the entry is a directory, make the directory
65 logger.debug("UnzipUtil.unzip_db: unzip and create widget folder {}", filePath);
66 File dir = new File(filePath);
67 stack.push(new File(filePath));
72 if(map.containsKey(entry.getName().substring(entry.getName().indexOf("/")+1))){
73 map.put(entry.getName().substring(entry.getName().indexOf("/")+1), Files.readAllBytes(Paths.get(filePath)));
76 entry = zipIn.getNextEntry();
79 while(!stack.isEmpty())
88 * Extracts a zip entry (file entry)
93 private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
94 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
95 byte[] bytesIn = new byte[BUFFER_SIZE];
97 while ((read = zipIn.read(bytesIn)) != -1) {
98 bos.write(bytesIn, 0, read);