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 if(!destination.parentFile.exists()) {
48 destination.parentFile.mkdirs()
50 destination.createNewFile()
51 ZipArchiveOutputStream(destination).use {
52 recurseFiles(source, source, it, absolute)
54 } catch (e: Exception) {
55 log.error("Fail to compress folder($source) to path(${destination.path})", e)
62 * Recursive traversal to add files
70 @Throws(IOException::class)
71 private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
73 if (file.isDirectory) {
75 val files = file.listFiles()
76 for (fileChild in files!!) {
77 recurseFiles(root, fileChild, zaos, absolute)
79 } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
80 val filename = if (absolute) {
81 file.absolutePath.substring(root.absolutePath.length)
85 val zae = ZipArchiveEntry(filename)
86 zae.size = file.length()
87 zaos.putArchiveEntry(zae)
88 FileInputStream(file).use {
89 IOUtils.copy(it, zaos)
92 zaos.closeArchiveEntry()
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()
105 if (entry.isDirectory)
108 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
110 destFilePath.outputStream().buffered(1024).use { bos ->
111 bufferedIs.copyTo(bos)
116 val destinationDir = File(targetPath)
117 check(destinationDir.isDirectory && destinationDir.exists()) {
118 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
121 return destinationDir