From 433947b5ab5e28fc29aee447de934de89a707419 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Thu, 19 Sep 2019 16:14:01 +0100 Subject: Centralize onboarding package validation Change-Id: I3cc58cf15f62008e83cfc7ddb095d07ab216b82a Issue-ID: SDC-2583 Signed-off-by: andre.schmid --- .../openecomp/core/utilities/file/FileUtils.java | 33 +++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java index 31338dcda4..f69a2a060a 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java @@ -229,9 +229,15 @@ public class FileUtils { public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws ZipException { final Map zipFileAndByteMap = ZipUtils.readZip(zipData, true); - final FileContentHandler mapFileContent = new FileContentHandler(); - mapFileContent.setFiles(zipFileAndByteMap); - return mapFileContent; + final FileContentHandler fileContentHandler = new FileContentHandler(); + zipFileAndByteMap.forEach((path, bytes) -> { + if (bytes == null) { + fileContentHandler.addFolder(path); + } else { + fileContentHandler.addFile(path, bytes); + } + }); + return fileContentHandler; } @@ -280,24 +286,23 @@ public class FileUtils { */ public static Map writeFilesFromFileContentHandler(final FileContentHandler fileContentHandler, final Path dir) throws IOException { - File file; final File dirFile = dir.toFile(); final Map filePaths = new HashMap<>(); + File file; + for (final String folderPath : fileContentHandler.getFolderList()) { + file = new File(dirFile, folderPath); + filePaths.put(folderPath, file.getAbsolutePath()); + if (!file.exists() && !file.mkdirs()) { + throw new IOException("Could not create directory " + file.getAbsolutePath()); + } + } for (final Map.Entry fileEntry : fileContentHandler.getFiles().entrySet()) { file = new File(dirFile, fileEntry.getKey()); filePaths.put(fileEntry.getKey(), file.getAbsolutePath()); final byte[] fileBytes = fileEntry.getValue(); - if (fileBytes == null) { - if (!file.exists() && !file.mkdirs()) { - throw new IOException("Could not create directory " + file.getAbsolutePath()); - } - continue; - } else { - if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { - throw new IOException("Could not create parent directory for " + file.getAbsolutePath()); - } + if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { + throw new IOException("Could not create parent directory for " + file.getAbsolutePath()); } - try (final FileOutputStream fop = new FileOutputStream(file.getAbsolutePath());) { fop.write(fileBytes); fop.flush(); -- cgit 1.2.3-korg