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.cds.controllerblueprints.core.utils
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
27 import java.io.FileInputStream
28 import java.io.IOException
29 import java.nio.charset.Charset
30 import java.util.zip.ZipFile
32 class BluePrintArchiveUtils {
35 private val log = LoggerFactory.getLogger(BluePrintArchiveUtils::class.java)
38 * Create a new Zip from a root directory
40 * @param source the base directory
41 * @param destination the output filename
42 * @param absolute store absolute filepath (from directory) or only filename
45 fun compress(source: File, destination: File, absolute: Boolean): Boolean {
47 destination.createNewFile()
48 ZipArchiveOutputStream(destination).use {
49 recurseFiles(source, source, it, absolute)
51 } catch (e: Exception) {
52 log.error("Fail to compress folder($source) to path(${destination.path})", e)
59 * Recursive traversal to add files
67 @Throws(IOException::class)
68 private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
70 if (file.isDirectory) {
72 val files = file.listFiles()
73 for (fileChild in files!!) {
74 recurseFiles(root, fileChild, zaos, absolute)
76 } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
77 val filename = if (absolute) {
78 file.absolutePath.substring(root.absolutePath.length)
82 val zae = ZipArchiveEntry(filename)
83 zae.size = file.length()
84 zaos.putArchiveEntry(zae)
85 FileInputStream(file).use {
86 IOUtils.copy(it, zaos)
89 zaos.closeArchiveEntry()
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()
102 if (entry.isDirectory)
105 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
107 destFilePath.outputStream().buffered(1024).use { bos ->
108 bufferedIs.copyTo(bos)
113 val destinationDir = File(targetPath)
114 check(destinationDir.isDirectory && destinationDir.exists()) {
115 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
118 return destinationDir