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
28 import java.nio.charset.Charset
29 import java.util.zip.ZipFile
31 class BluePrintArchiveUtils {
35 fun getFileContent(fileName: String): String = runBlocking {
38 File(fileName).readText(Charsets.UTF_8)
39 } catch (e: Exception) {
40 throw BluePrintException("couldn't find file($fileName)")
45 fun compress(source: String, destination: String, absolute: Boolean): Boolean {
46 val rootDir = File(source)
47 val saveFile = File(destination)
48 return compress(rootDir, saveFile, absolute)
52 * Create a new Zip from a root directory
54 * @param directory the base directory
55 * @param filename the output filename
56 * @param absolute store absolute filepath (from directory) or only filename
59 fun compress(source: File, destination: File, absolute: Boolean): Boolean {
61 val zaos: ZipArchiveOutputStream
63 zaos = ZipArchiveOutputStream(FileOutputStream(destination))
64 } catch (e: FileNotFoundException) {
69 recurseFiles(source, source, zaos, absolute)
70 } catch (e2: IOException) {
73 } catch (e: IOException) {
82 } catch (e1: IOException) {
88 } catch (e: IOException) {
94 } catch (e: IOException) {
102 * Recursive traversal to add files
108 * @throws IOException
110 @Throws(IOException::class)
111 private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
113 if (file.isDirectory) {
115 val files = file.listFiles()
116 for (file2 in files!!) {
117 recurseFiles(root, file2, zaos, absolute)
119 } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
120 var filename: String? = null
122 filename = file.absolutePath.substring(root.absolutePath.length)
126 val zae = ZipArchiveEntry(filename)
127 zae.setSize(file.length())
128 zaos.putArchiveEntry(zae)
129 val fis = FileInputStream(file)
130 IOUtils.copy(fis, zaos)
131 zaos.closeArchiveEntry()
136 fun deCompress(zipFile: File, targetPath: String): File {
137 val zip = ZipFile(zipFile, Charset.defaultCharset())
138 val enumeration = zip.entries()
139 while (enumeration.hasMoreElements()) {
140 val entry = enumeration.nextElement()
141 val destFilePath = File(targetPath, entry.name)
142 destFilePath.parentFile.mkdirs()
144 if (entry.isDirectory)
147 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
149 destFilePath.outputStream().buffered(1024).use { bos ->
150 bufferedIs.copyTo(bos)
155 val destinationDir = File(targetPath)
156 check(destinationDir.isDirectory && destinationDir.exists()) {
157 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
160 return destinationDir
164 * Get the first item in directory
169 fun getFirstItemInDirectory(dir: File): String {
170 return dir.walk().map { it.name }.elementAt(1)