exception is thrown on import normative

Change-Id: I26cd77466d4c79836665929066d2bff5b6ead92b
Issue-ID: SDC-1796
Signed-off-by: Tal Gitelman <tg851x@intl.att.com>
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java
index c403520..14fc77a 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java
@@ -169,9 +169,8 @@
 
     }
 
-    protected void validateZip(Wrapper<Response> responseWrapper, File file, String payloadName) throws FileNotFoundException {
-        InputStream fileInputStream = new FileInputStream(file);
-        Map<String, byte[]> unzippedFolder = ZipUtil.readZip(new ZipInputStream(fileInputStream));
+    protected void validateZip(Wrapper<Response> responseWrapper, File file, String payloadName) {
+        Map<String, byte[]> unzippedFolder = ZipUtil.readZip(file);
         if (payloadName == null || payloadName.isEmpty() || !unzippedFolder.containsKey(payloadName)) {
             log.info("Invalid json was received. payloadName should be yml file name");
             Response errorResponse = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
@@ -179,9 +178,8 @@
         }
 
     }
-    protected void validateCsar(Wrapper<Response> responseWrapper, File file, String payloadName) throws FileNotFoundException {
-        InputStream fileInputStream = new FileInputStream(file);
-        Map<String, byte[]> unzippedFolder = ZipUtil.readZip(new ZipInputStream(fileInputStream));
+    protected void validateCsar(Wrapper<Response> responseWrapper, File file, String payloadName) {
+        Map<String, byte[]> unzippedFolder = ZipUtil.readZip(file);
         if (payloadName == null || payloadName.isEmpty() || unzippedFolder.isEmpty()) {
             log.info("Invalid json was received. payloadName should be yml file name");
             Response errorResponse = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
@@ -190,20 +188,18 @@
 
     }
 
-    protected void fillZipContents(Wrapper<String> yamlStringWrapper, File file) throws FileNotFoundException {
+    protected void fillZipContents(Wrapper<String> yamlStringWrapper, File file) {
         extractZipContents(yamlStringWrapper, file);
     }
 
-    public static void extractZipContents(Wrapper<String> yamlStringWrapper, File file) throws FileNotFoundException {
-        InputStream fileInputStream = new FileInputStream(file);
-        Map<String, byte[]> unzippedFolder = ZipUtil.readZip(new ZipInputStream(fileInputStream));
+    public static void extractZipContents(Wrapper<String> yamlStringWrapper, File file) {
+        Map<String, byte[]> unzippedFolder = ZipUtil.readZip(file);
         String ymlName = unzippedFolder.keySet().iterator().next();
         fillToscaTemplateFromZip(yamlStringWrapper, ymlName, file);
     }
 
-    private static void fillToscaTemplateFromZip(Wrapper<String> yamlStringWrapper, String payloadName, File file) throws FileNotFoundException {
-        InputStream fileInputStream = new FileInputStream(file);
-        Map<String, byte[]> unzippedFolder = ZipUtil.readZip(new ZipInputStream(fileInputStream));
+    private static void fillToscaTemplateFromZip(Wrapper<String> yamlStringWrapper, String payloadName, File file) {
+        Map<String, byte[]> unzippedFolder = ZipUtil.readZip(file);
         byte[] yamlFileInBytes = unzippedFolder.get(payloadName);
         String yamlAsString = new String(yamlFileInBytes, StandardCharsets.UTF_8);
         log.debug("received yaml: {}", yamlAsString);
diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/util/ZipUtil.java b/common-app-api/src/main/java/org/openecomp/sdc/common/util/ZipUtil.java
index 2711a29..2036e23 100644
--- a/common-app-api/src/main/java/org/openecomp/sdc/common/util/ZipUtil.java
+++ b/common-app-api/src/main/java/org/openecomp/sdc/common/util/ZipUtil.java
@@ -20,11 +20,11 @@
 
 package org.openecomp.sdc.common.util;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.openecomp.sdc.common.log.wrappers.Logger;
 
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
+import java.io.*;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -41,20 +41,20 @@
 	private ZipUtil() {
 	}
 
+	public static Map<String, byte[]> readZip(File file) {
+        try(InputStream fileInputStream = new FileInputStream(file)){
+	        return readZip(IOUtils.toByteArray(fileInputStream));
+        } catch (IOException e) {
+            log.info("close File stream failed - {}" , e);
+            return null;
+        }
+    }
+
 	public static Map<String, byte[]> readZip(byte[] zipAsBytes) {
-
-		ZipInputStream zis = null;
-		zis = new ZipInputStream(new ByteArrayInputStream(zipAsBytes));
-
-		return readZip(zis);
-	}
-
-	public static Map<String, byte[]> readZip(ZipInputStream zis) {
-
 		Map<String, byte[]> fileNameToByteArray = new HashMap<>();
-
 		byte[] buffer = new byte[1024];
-		try {
+        try(ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipAsBytes);
+			ZipInputStream zis = new ZipInputStream(byteArrayInputStream)) {
 			// get the zipped file list entry
 			ZipEntry ze = zis.getNextEntry();
 
@@ -75,25 +75,10 @@
 					}
 				}
 				ze = zis.getNextEntry();
-
 			}
-
-			zis.closeEntry();
-			zis.close();
-
 		} catch (IOException ex) {
-			log.info("close Byte stream failed - {}" , ex);
+			log.info("close Byte stream failed" , ex);
 			return null;
-		} finally {
-			if (zis != null) {
-				try {
-					zis.closeEntry();
-					zis.close();
-				} catch (IOException e) {
-					log.info("Close ZipInputStream failed - {}" , e);
-				}
-
-			}
 		}
 
 		return fileNameToByteArray;
@@ -115,7 +100,7 @@
 			ZipUtil.readZip(zipAsBytes);
 
 		} catch (IOException e) {
-			log.info("close Byte stream failed - {}" , e);
+			log.info("close Byte stream failed" , e);
 		}
 
 	}