aboutsummaryrefslogtreecommitdiffstats
path: root/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintArchiveUtils.kt
blob: 1176f713dd03cee8090d5114c2b53b7fb73eb559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2019 Bell Canada.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.apps.controllerblueprints.core.utils

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
import org.apache.commons.io.IOUtils
import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
import org.slf4j.LoggerFactory
import java.io.BufferedInputStream
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.nio.charset.Charset
import java.util.zip.ZipFile

class BluePrintArchiveUtils {

    companion object {
        private val log = LoggerFactory.getLogger(BluePrintArchiveUtils::class.java)

        /**
         * Create a new Zip from a root directory
         *
         * @param source the base directory
         * @param destination the output filename
         * @param absolute store absolute filepath (from directory) or only filename
         * @return True if OK
         */
        fun compress(source: File, destination: File, absolute: Boolean): Boolean {
            try {
                destination.createNewFile()
                ZipArchiveOutputStream(destination).use {
                    recurseFiles(source, source, it, absolute)
                }
            } catch (e: Exception) {
                log.error("Fail to compress folder($source) to path(${destination.path})", e)
                return false
            }
            return true
        }

        /**
         * Recursive traversal to add files
         *
         * @param root
         * @param file
         * @param zaos
         * @param absolute
         * @throws IOException
         */
        @Throws(IOException::class)
        private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
                                 absolute: Boolean) {
            if (file.isDirectory) {
                // recursive call
                val files = file.listFiles()
                for (fileChild in files!!) {
                    recurseFiles(root, fileChild, zaos, absolute)
                }
            } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
                val filename = if (absolute) {
                    file.absolutePath.substring(root.absolutePath.length)
                } else {
                    file.name
                }
                val zae = ZipArchiveEntry(filename)
                zae.size = file.length()
                zaos.putArchiveEntry(zae)
                FileInputStream(file).use {
                    IOUtils.copy(it, zaos)
                    it.close()
                }
                zaos.closeArchiveEntry()
            }
        }


        fun deCompress(zipFile: File, targetPath: String): File {
            val zip = ZipFile(zipFile, Charset.defaultCharset())
            val enumeration = zip.entries()
            while (enumeration.hasMoreElements()) {
                val entry = enumeration.nextElement()
                val destFilePath = File(targetPath, entry.name)
                destFilePath.parentFile.mkdirs()

                if (entry.isDirectory)
                    continue

                val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
                bufferedIs.use {
                    destFilePath.outputStream().buffered(1024).use { bos ->
                        bufferedIs.copyTo(bos)
                    }
                }
            }

            val destinationDir = File(targetPath)
            check(destinationDir.isDirectory && destinationDir.exists()) {
                throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
            }

            return destinationDir
        }
    }

}