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)
41 * Create a new Zip from a root directory
43 * @param source the base directory
44 * @param destination the output filename
45 * @param absolute store absolute filepath (from directory) or only filename
48 fun compress(source: File, destination: File, absolute: Boolean): Boolean {
50 destination.createNewFile()
51 ZipArchiveOutputStream(destination).use {
52 recurseFiles(source, source, it, absolute)
54 } catch (e: Exception) {
55 log.error("Fail to compress folder($source) to path(${destination.path}", e)
62 * Recursive traversal to add files
70 @Throws(IOException::class)
71 private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
73 if (file.isDirectory) {
75 val files = file.listFiles()
76 for (fileChild in files!!) {
77 recurseFiles(root, fileChild, zaos, absolute)
79 } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
80 val filename = if (absolute) {
81 file.absolutePath.substring(root.absolutePath.length)
85 val zae = ZipArchiveEntry(filename)
86 zae.size = file.length()
87 zaos.putArchiveEntry(zae)
88 FileInputStream(file).use {
89 IOUtils.copy(it, zaos)
92 zaos.closeArchiveEntry()
97 fun deCompress(zipFile: File, targetPath: String): File {
98 val zip = ZipFile(zipFile, Charset.defaultCharset())
99 val enumeration = zip.entries()
100 while (enumeration.hasMoreElements()) {
101 val entry = enumeration.nextElement()
102 val destFilePath = File(targetPath, entry.name)
103 destFilePath.parentFile.mkdirs()
105 if (entry.isDirectory)
108 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
110 destFilePath.outputStream().buffered(1024).use { bos ->
111 bufferedIs.copyTo(bos)
116 val destinationDir = File(targetPath)
117 check(destinationDir.isDirectory && destinationDir.exists()) {
118 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
121 return destinationDir