package org.onap.ccsdk.apps.controllerblueprints.core.utils
-import kotlinx.coroutines.async
-import kotlinx.coroutines.runBlocking
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
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 org.slf4j.LoggerFactory
import java.io.BufferedInputStream
companion object {
private val log = LoggerFactory.getLogger(BluePrintArchiveUtils::class.java)
- fun getFileContent(fileName: String): String = runBlocking {
- async {
- try {
- File(fileName).readText(Charsets.UTF_8)
- } catch (e: Exception) {
- throw BluePrintException("couldn't find file($fileName)")
- }
- }.await()
- }
-
- fun compress(source: String, destination: String, absolute: Boolean): Boolean {
- val rootDir = File(source)
- val saveFile = File(destination)
- return compress(rootDir, saveFile, absolute)
- }
-
/**
* Create a new Zip from a root directory
*
*/
fun compress(source: File, destination: File, absolute: Boolean): Boolean {
try {
+ destination.createNewFile()
ZipArchiveOutputStream(destination).use {
recurseFiles(source, source, it, absolute)
}
} catch (e: Exception) {
- log.error("Fail to compress folder(:$source) to path(${destination.path}", e)
+ log.error("Fail to compress folder($source) to path(${destination.path})", e)
return false
}
return true
val zae = ZipArchiveEntry(filename)
zae.size = file.length()
zaos.putArchiveEntry(zae)
- FileInputStream(file).use { IOUtils.copy(it, zaos) }
+ FileInputStream(file).use {
+ IOUtils.copy(it, zaos)
+ it.close()
+ }
zaos.closeArchiveEntry()
}
}
return destinationDir
}
-
- /**
- * Get the first item in directory
- *
- * @param zipFile
- * @return string
- */
- fun getFirstItemInDirectory(dir: File): String {
- return dir.walk().map { it.name }.elementAt(1)
- }
}
}
\ No newline at end of file