Migrate "ms/controllerblueprints" from ccsdk/apps
[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                 destination.createNewFile()
48                 ZipArchiveOutputStream(destination).use {
49                     recurseFiles(source, source, it, absolute)
50                 }
51             } catch (e: Exception) {
52                 log.error("Fail to compress folder($source) to path(${destination.path})", e)
53                 return false
54             }
55             return true
56         }
57
58         /**
59          * Recursive traversal to add files
60          *
61          * @param root
62          * @param file
63          * @param zaos
64          * @param absolute
65          * @throws IOException
66          */
67         @Throws(IOException::class)
68         private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
69                                  absolute: Boolean) {
70             if (file.isDirectory) {
71                 // recursive call
72                 val files = file.listFiles()
73                 for (fileChild in files!!) {
74                     recurseFiles(root, fileChild, zaos, absolute)
75                 }
76             } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
77                 val filename = if (absolute) {
78                     file.absolutePath.substring(root.absolutePath.length)
79                 } else {
80                     file.name
81                 }
82                 val zae = ZipArchiveEntry(filename)
83                 zae.size = file.length()
84                 zaos.putArchiveEntry(zae)
85                 FileInputStream(file).use {
86                     IOUtils.copy(it, zaos)
87                     it.close()
88                 }
89                 zaos.closeArchiveEntry()
90             }
91         }
92
93
94         fun deCompress(zipFile: File, targetPath: String): File {
95             val zip = ZipFile(zipFile, Charset.defaultCharset())
96             val enumeration = zip.entries()
97             while (enumeration.hasMoreElements()) {
98                 val entry = enumeration.nextElement()
99                 val destFilePath = File(targetPath, entry.name)
100                 destFilePath.parentFile.mkdirs()
101
102                 if (entry.isDirectory)
103                     continue
104
105                 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
106                 bufferedIs.use {
107                     destFilePath.outputStream().buffered(1024).use { bos ->
108                         bufferedIs.copyTo(bos)
109                     }
110                 }
111             }
112
113             val destinationDir = File(targetPath)
114             check(destinationDir.isDirectory && destinationDir.exists()) {
115                 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
116             }
117
118             return destinationDir
119         }
120     }
121
122 }