Merge "Fixed restconf Python scripts bugs caused by BPP refactor"
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / utils / BluePrintArchiveUtils.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.utils
19
20 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
21 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
22 import org.apache.commons.io.IOUtils
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
24 import org.slf4j.LoggerFactory
25 import java.io.BufferedInputStream
26 import java.io.File
27 import java.io.FileInputStream
28 import java.io.IOException
29 import java.nio.charset.Charset
30 import java.util.zip.ZipFile
31
32 class BluePrintArchiveUtils {
33
34     companion object {
35         private val log = LoggerFactory.getLogger(BluePrintArchiveUtils::class.java)
36
37         /**
38          * Create a new Zip from a root directory
39          *
40          * @param source the base directory
41          * @param destination the output filename
42          * @param absolute store absolute filepath (from directory) or only filename
43          * @return True if OK
44          */
45         fun compress(source: File, destination: File, absolute: Boolean): Boolean {
46             try {
47                 if(!destination.parentFile.exists()) {
48                     destination.parentFile.mkdirs()
49                 }
50                 destination.createNewFile()
51                 ZipArchiveOutputStream(destination).use {
52                     recurseFiles(source, source, it, absolute)
53                 }
54             } catch (e: Exception) {
55                 log.error("Fail to compress folder($source) to path(${destination.path})", e)
56                 return false
57             }
58             return true
59         }
60
61         /**
62          * Recursive traversal to add files
63          *
64          * @param root
65          * @param file
66          * @param zaos
67          * @param absolute
68          * @throws IOException
69          */
70         @Throws(IOException::class)
71         private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
72                                  absolute: Boolean) {
73             if (file.isDirectory) {
74                 // recursive call
75                 val files = file.listFiles()
76                 for (fileChild in files!!) {
77                     recurseFiles(root, fileChild, zaos, absolute)
78                 }
79             } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
80                 val filename = if (absolute) {
81                     file.absolutePath.substring(root.absolutePath.length)
82                 } else {
83                     file.name
84                 }
85                 val zae = ZipArchiveEntry(filename)
86                 zae.size = file.length()
87                 zaos.putArchiveEntry(zae)
88                 FileInputStream(file).use {
89                     IOUtils.copy(it, zaos)
90                     it.close()
91                 }
92                 zaos.closeArchiveEntry()
93             }
94         }
95
96
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()
104
105                 if (entry.isDirectory)
106                     continue
107
108                 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
109                 bufferedIs.use {
110                     destFilePath.outputStream().buffered(1024).use { bos ->
111                         bufferedIs.copyTo(bos)
112                     }
113                 }
114             }
115
116             val destinationDir = File(targetPath)
117             check(destinationDir.isDirectory && destinationDir.exists()) {
118                 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
119             }
120
121             return destinationDir
122         }
123     }
124
125 }