2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
4 * Modifications Copyright © 2019 Nordix Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.controllerblueprints.core.utils
21 import com.google.common.base.Predicates
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.slf4j.LoggerFactory
24 import java.io.BufferedInputStream
25 import java.io.ByteArrayOutputStream
27 import java.io.FileOutputStream
28 import java.io.IOException
29 import java.io.OutputStream
30 import java.nio.charset.Charset
31 import java.nio.file.FileVisitResult
32 import java.nio.file.Files
33 import java.nio.file.Path
34 import java.nio.file.SimpleFileVisitor
35 import java.nio.file.attribute.BasicFileAttributes
36 import java.util.function.Predicate
37 import java.util.zip.Deflater
38 import java.util.zip.ZipEntry
39 import java.util.zip.ZipFile
40 import java.util.zip.ZipOutputStream
42 class BluePrintArchiveUtils {
45 private val log = LoggerFactory.getLogger(BluePrintArchiveUtils::class.java)
48 * Create a new Zip from a root directory
50 * @param source the base directory
51 * @param destination the output filename
54 fun compress(source: File, destination: File): Boolean {
56 destination.createNewFile()
57 val ignoreZipFiles = Predicate<Path> { path -> !path.endsWith(".zip") && !path.endsWith(".ZIP") }
58 FileOutputStream(destination).use { out ->
59 compressFolder(source.toPath(), out, pathFilter = ignoreZipFiles)
61 } catch (e: Exception) {
62 log.error("Fail to compress folder($source) to path(${destination.path})", e)
69 * In-memory compress an entire folder.
71 fun compressToBytes(baseDir: Path, compressionLevel: Int = Deflater.NO_COMPRESSION): ByteArray {
72 return compressFolder(baseDir, ByteArrayOutputStream(), compressionLevel = compressionLevel)
77 * Compress an entire folder.
79 * @param baseDir path of base folder to be packaged.
80 * @param output the output stream
81 * @param pathFilter filter to ignore files based on its path.
82 * @param compressionLevel the wanted compression level.
83 * @param fixedModificationTime to force every entry to have this modification time.
84 * Useful for reproducible operations, like tests, for example.
86 private fun <T> compressFolder(baseDir: Path, output: T,
87 pathFilter: Predicate<Path> = Predicates.alwaysTrue(),
88 compressionLevel: Int = Deflater.DEFAULT_COMPRESSION,
89 fixedModificationTime: Long? = null): T
90 where T : OutputStream {
91 ZipOutputStream(output)
92 .apply { setLevel(compressionLevel) }
94 Files.walkFileTree(baseDir, object : SimpleFileVisitor<Path>() {
95 @Throws(IOException::class)
96 override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
97 if (pathFilter.test(file)) {
98 val zipEntry = ZipEntry(baseDir.relativize(file).toString())
99 fixedModificationTime?.let {
103 zos.putNextEntry(zipEntry)
104 Files.copy(file, zos)
107 return FileVisitResult.CONTINUE
110 @Throws(IOException::class)
111 override fun preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult {
112 val zipEntry = ZipEntry(baseDir.relativize(dir).toString() + "/")
113 fixedModificationTime?.let {
116 zos.putNextEntry(zipEntry)
118 return FileVisitResult.CONTINUE
125 fun deCompress(zipFile: File, targetPath: String): File {
126 val zip = ZipFile(zipFile, Charset.defaultCharset())
127 val enumeration = zip.entries()
128 while (enumeration.hasMoreElements()) {
129 val entry = enumeration.nextElement()
130 val destFilePath = File(targetPath, entry.name)
131 destFilePath.parentFile.mkdirs()
133 if (entry.isDirectory)
136 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
138 destFilePath.outputStream().buffered(1024).use { bos ->
139 bufferedIs.copyTo(bos)
144 val destinationDir = File(targetPath)
145 check(destinationDir.isDirectory && destinationDir.exists()) {
146 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
149 return destinationDir