1 package org.onap.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.onap.portalapp.widget.constant.WidgetConstant;
17 import org.slf4j.Logger;
18 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);
29 * Extracts a zip file specified by the zipFilePath to a directory specified by
30 * destDirectory (will be created if does not exists)
34 * @param destDirectory
38 * @return Map of contents
42 public Map<String, byte[]> unzip_db(String zipFilePath, String destDirectory, String widgetName)
45 logger.debug("UnzipUtil.unzip_db: unzip widget file {}", widgetName);
46 File destDir = new File(destDirectory);
47 if (!destDir.exists())
49 try(ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)))
51 ZipEntry entry = zipIn.getNextEntry();
52 Map<String, byte[]> map = new HashMap<>();
54 String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION,
55 WidgetConstant.WIDGET_STYLE_LOCATION };
56 for (String k : requiredKeys)
59 // iterates over entries in the zip file
60 Stack<File> stack = new Stack<>();
61 while (entry != null) {
62 String filePath = destDirectory + File.separator + widgetName + File.separator
63 + entry.getName().substring(entry.getName().indexOf("/") + 1);
64 final String entryShortName = entry.getName().substring(entry.getName().indexOf("/") + 1);
65 logger.debug("UnzipUtil.unzip_db: file path {}, short name {}", filePath, entryShortName);
66 if (!entry.isDirectory()) {
67 // if the entry is a file, extracts it
68 logger.debug("UnzipUtil.unzip_db: unzip and save widget file {}", filePath);
69 stack.push(new File(filePath));
70 extractFile(zipIn, filePath);
72 // if the entry is a directory, make the directory
73 logger.debug("UnzipUtil.unzip_db: unzip and create widget folder {}", filePath);
74 File dir = new File(filePath);
75 stack.push(new File(filePath));
78 // Is this one we need?
79 if (map.containsKey(entryShortName))
80 map.put(entryShortName, Files.readAllBytes(Paths.get(filePath)));
82 entry = zipIn.getNextEntry();
85 while (!stack.isEmpty())
88 for (String k : requiredKeys)
89 if (!map.containsKey(k))
90 logger.warn("UnzipUtil.unzip_db: no zip archive entry found for required key {}", k);
97 * Extracts a zip entry (file entry)
101 * @throws IOException
103 private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
104 try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))){
105 byte[] bytesIn = new byte[BUFFER_SIZE];
107 while ((read = zipIn.read(bytesIn)) != -1) {
108 bos.write(bytesIn, 0, read);