2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.apps.controllerblueprints.core.utils
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
24 import java.nio.charset.Charset
25 import java.util.zip.ZipFile
27 class BluePrintArchiveUtils {
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)
39 * Create a new Zip from a root directory
41 * @param directory the base directory
42 * @param filename the output filename
43 * @param absolute store absolute filepath (from directory) or only filename
46 fun compress(source: File, destination: File, absolute: Boolean): Boolean {
48 val zaos: ZipArchiveOutputStream
50 zaos = ZipArchiveOutputStream(FileOutputStream(destination))
51 } catch (e: FileNotFoundException) {
56 recurseFiles(source, source, zaos, absolute)
57 } catch (e2: IOException) {
60 } catch (e: IOException) {
69 } catch (e1: IOException) {
75 } catch (e: IOException) {
81 } catch (e: IOException) {
89 * Recursive traversal to add files
97 @Throws(IOException::class)
98 private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream,
100 if (file.isDirectory) {
102 val files = file.listFiles()
103 for (file2 in files!!) {
104 recurseFiles(root, file2, zaos, absolute)
106 } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
107 var filename: String? = null
109 filename = file.absolutePath.substring(root.absolutePath.length)
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()
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)
132 val bufferedIs = BufferedInputStream(zip.getInputStream(entry))
134 destFilePath.outputStream().buffered(1024).use { bos ->
135 bufferedIs.copyTo(bos)
140 val destinationDir = File(targetPath)
141 check(destinationDir.isDirectory && destinationDir.exists()) {
142 throw BluePrintProcessorException("failed to decompress blueprint(${zipFile.absolutePath}) to ($targetPath) ")
144 return destinationDir