From bece50a90f6510f0ec3d109ff4dc4a9257881ac1 Mon Sep 17 00:00:00 2001 From: Alexis de Talhouët Date: Tue, 15 Jan 2019 14:44:58 -0500 Subject: Implement CBA upload through REST MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I417254c5107f8b0031932e6a7cf0599561ee9a3c Issue-ID: CCSDK-910 Signed-off-by: Alexis de Talhouët --- .../core/utils/BluePrintArchiveUtils.kt | 65 +++++++--------------- 1 file changed, 19 insertions(+), 46 deletions(-) diff --git a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintArchiveUtils.kt b/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintArchiveUtils.kt index ab5175dea..fe7929e85 100755 --- a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintArchiveUtils.kt +++ b/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintArchiveUtils.kt @@ -24,13 +24,18 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream import org.apache.commons.io.IOUtils import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException -import java.io.* +import org.slf4j.LoggerFactory +import java.io.BufferedInputStream +import java.io.File +import java.io.FileInputStream +import java.io.IOException import java.nio.charset.Charset import java.util.zip.ZipFile class BluePrintArchiveUtils { companion object { + private val log = LoggerFactory.getLogger(BluePrintArchiveUtils::class.java) fun getFileContent(fileName: String): String = runBlocking { async { @@ -51,50 +56,20 @@ class BluePrintArchiveUtils { /** * Create a new Zip from a root directory * - * @param directory the base directory - * @param filename the output filename + * @param source the base directory + * @param destination the output filename * @param absolute store absolute filepath (from directory) or only filename * @return True if OK */ fun compress(source: File, destination: File, absolute: Boolean): Boolean { - // recursive call - val zaos: ZipArchiveOutputStream try { - zaos = ZipArchiveOutputStream(FileOutputStream(destination)) - } catch (e: FileNotFoundException) { - return false - } - - try { - recurseFiles(source, source, zaos, absolute) - } catch (e2: IOException) { - try { - zaos.close() - } catch (e: IOException) { - // ignore + ZipArchiveOutputStream(destination).use { + recurseFiles(source, source, it, absolute) } - + } catch (e: Exception) { + log.error("Fail to compress folder(:$source) to path(${destination.path}", e) return false } - - try { - zaos.finish() - } catch (e1: IOException) { - // ignore - } - - try { - zaos.flush() - } catch (e: IOException) { - // ignore - } - - try { - zaos.close() - } catch (e: IOException) { - // ignore - } - return true } @@ -113,21 +88,19 @@ class BluePrintArchiveUtils { if (file.isDirectory) { // recursive call val files = file.listFiles() - for (file2 in files!!) { - recurseFiles(root, file2, zaos, absolute) + for (fileChild in files!!) { + recurseFiles(root, fileChild, zaos, absolute) } } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) { - var filename: String? = null - if (absolute) { - filename = file.absolutePath.substring(root.absolutePath.length) + val filename = if (absolute) { + file.absolutePath.substring(root.absolutePath.length) } else { - filename = file.name + file.name } val zae = ZipArchiveEntry(filename) - zae.setSize(file.length()) + zae.size = file.length() zaos.putArchiveEntry(zae) - val fis = FileInputStream(file) - IOUtils.copy(fis, zaos) + FileInputStream(file).use { IOUtils.copy(it, zaos) } zaos.closeArchiveEntry() } } -- cgit 1.2.3-korg