f02524ffa4eac9cf8c061d543e6ba750a842c488
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.core.utils
18
19 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
20 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
21 import org.apache.commons.io.IOUtils
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
23 import java.io.*
24 import java.nio.charset.Charset
25 import java.util.zip.ZipFile
26
27 class BluePrintArchiveUtils {
28
29     companion object {
30
31
32         fun compress(source: String, destination: String, absolute: Boolean): Boolean {
33             val rootDir = File(source)
34             val saveFile = File(destination)
35             return compress(rootDir, saveFile, absolute)
36         }
37
38         /**
39          * Create a new Zip from a root directory
40          *
41          * @param directory the base directory
42          * @param filename the output filename
43          * @param absolute store absolute filepath (from directory) or only filename
44          * @return True if OK
45          */
46         fun compress(source: File, destination: File, absolute: Boolean): Boolean {
47             // recursive call
48             val zaos: ZipArchiveOutputStream
49             try {
50                 zaos = ZipArchiveOutputStream(FileOutputStream(destination))
51             } catch (e: FileNotFoundException) {
52                 return false
53             }
54
55             try {
56                 recurseFiles(source, source, zaos, absolute)
57             } catch (e2: IOException) {
58                 try {
59                     zaos.close()
60                 } catch (e: IOException) {
61                     // ignore
62                 }
63
64                 return false
65             }
66
67             try {
68                 zaos.finish()
69             } catch (e1: IOException) {
70                 // ignore
71             }
72
73             try {
74                 zaos.flush()
75             } catch (e: IOException) {
76                 // ignore
77             }
78
79             try {
80                 zaos.close()
81             } catch (e: IOException) {
82                 // ignore
83             }
84
85             return true
86         }
87
88         /**
89          * Recursive traversal to add files
90          *
91          * @param root
92          * @param file
93          * @param zaos
94          * @param absolute
95          * @throws IOException
96          */
97         @Throws(IOException::class)
98         private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
99                                  absolute: Boolean) {
100             if (file.isDirectory) {
101                 // recursive call
102                 val files = file.listFiles()
103                 for (file2 in files!!) {
104                     recurseFiles(root, file2, zaos, absolute)
105                 }
106             } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
107                 var filename: String? = null
108                 if (absolute) {
109                     filename = file.absolutePath.substring(root.absolutePath.length)
110                 } else {
111                     filename = file.name
112                 }
113                 val zae = ZipArchiveEntry(filename)
114                 zae.setSize(file.length())
115                 zaos.putArchiveEntry(zae)
116                 val fis = FileInputStream(file)
117                 IOUtils.copy(fis, zaos)
118                 zaos.closeArchiveEntry()
119             }
120         }
121
122
123         fun deCompress(zipFile: File, targetPath: String): File {
124             val zip = ZipFile(zipFile, Charset.defaultCharset())
125             val enumeration = zip.entries()
126             while (enumeration.hasMoreElements()) {
127                 val entry = enumeration.nextElement()
128                 val destFilePath = File(targetPath, entry.name)
129                 destFilePath.parentFile.mkdirs()
130                 if (entry.isDirectory)
131                     continue
132                 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
133                 bufferedIs.use {
134                     destFilePath.outputStream().buffered(1024).use { bos ->
135                         bufferedIs.copyTo(bos)
136                     }
137                 }
138             }
139
140             val destinationDir = File(targetPath)
141             check(destinationDir.isDirectory && destinationDir.exists()) {
142                 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
143             }
144             return destinationDir
145         }
146     }
147
148 }