summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorDan Timoney <dtimoney@att.com>2019-01-19 01:04:38 +0000
committerGerrit Code Review <gerrit@onap.org>2019-01-19 01:04:38 +0000
commit5aba70d9b3118acd5b2250f25a793566a8e627b5 (patch)
treeefa54a82746db0f0d28215bd1054bd3ad0a57ca2 /components
parentdc164ad587dc78cdbe06b7b8cdef2da7f952eb68 (diff)
parentc2cbc1a708ce82678e8177ca4985d5324f7d027f (diff)
Merge "Implement CBA upload through REST"
Diffstat (limited to 'components')
-rwxr-xr-xcomponents/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintArchiveUtils.kt65
1 files 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 ab5175de..fe7929e8 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()
}
}