2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.apps.controllerblueprints.core.utils
20 import kotlinx.coroutines.async
21 import kotlinx.coroutines.runBlocking
22 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
23 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
24 import org.apache.commons.io.IOUtils
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
27 import org.slf4j.LoggerFactory
28 import java.io.BufferedInputStream
30 import java.io.FileInputStream
31 import java.io.IOException
32 import java.nio.charset.Charset
33 import java.util.zip.ZipFile
35 class BluePrintArchiveUtils {
38 private val log = LoggerFactory.getLogger(BluePrintArchiveUtils::class.java)
40 fun getFileContent(fileName: String): String = runBlocking {
43 File(fileName).readText(Charsets.UTF_8)
44 } catch (e: Exception) {
45 throw BluePrintException("couldn't find file($fileName)")
50 fun compress(source: String, destination: String, absolute: Boolean): Boolean {
51 val rootDir = File(source)
52 val saveFile = File(destination)
53 return compress(rootDir, saveFile, absolute)
57 * Create a new Zip from a root directory
59 * @param source the base directory
60 * @param destination the output filename
61 * @param absolute store absolute filepath (from directory) or only filename
64 fun compress(source: File, destination: File, absolute: Boolean): Boolean {
66 ZipArchiveOutputStream(destination).use {
67 recurseFiles(source, source, it, absolute)
69 } catch (e: Exception) {
70 log.error("Fail to compress folder(:$source) to path(${destination.path}", e)
77 * Recursive traversal to add files
85 @Throws(IOException::class)
86 private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
88 if (file.isDirectory) {
90 val files = file.listFiles()
91 for (fileChild in files!!) {
92 recurseFiles(root, fileChild, zaos, absolute)
94 } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
95 val filename = if (absolute) {
96 file.absolutePath.substring(root.absolutePath.length)
100 val zae = ZipArchiveEntry(filename)
101 zae.size = file.length()
102 zaos.putArchiveEntry(zae)
103 FileInputStream(file).use { IOUtils.copy(it, zaos) }
104 zaos.closeArchiveEntry()
109 fun deCompress(zipFile: File, targetPath: String): File {
110 val zip = ZipFile(zipFile, Charset.defaultCharset())
111 val enumeration = zip.entries()
112 while (enumeration.hasMoreElements()) {
113 val entry = enumeration.nextElement()
114 val destFilePath = File(targetPath, entry.name)
115 destFilePath.parentFile.mkdirs()
117 if (entry.isDirectory)
120 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
122 destFilePath.outputStream().buffered(1024).use { bos ->
123 bufferedIs.copyTo(bos)
128 val destinationDir = File(targetPath)
129 check(destinationDir.isDirectory && destinationDir.exists()) {
130 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
133 return destinationDir
137 * Get the first item in directory
142 fun getFirstItemInDirectory(dir: File): String {
143 return dir.walk().map { it.name }.elementAt(1)