a433a31b1348d57d3855e7c6cd49caadc4475cca
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
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
18
19 import org.apache.commons.io.FileUtils
20 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils
21 import java.io.File
22 import java.io.InputStream
23 import java.nio.file.Paths
24
25 fun InputStream.toFile(path: String): File {
26     val file = File(path)
27     file.outputStream().use { this.copyTo(it) }
28     return file
29 }
30
31 fun File.reCreateDirs(): File {
32     if (this.exists()) {
33         this.deleteRecursively()
34     }
35     // this.mkdirs()
36     FileUtils.forceMkdir(this)
37     check(this.exists()) {
38         throw BluePrintException("failed to re create dir(${this.absolutePath})")
39     }
40     return this
41 }
42
43 fun File.compress(targetZipFileName: String): File {
44     return this.compress(Paths.get(targetZipFileName).toFile())
45 }
46
47 /**
48  * Compress the current Dir to the target zip file and return the target zip file
49  */
50 fun File.compress(targetZipFile: File): File {
51     BluePrintArchiveUtils.compress(this, targetZipFile, true)
52     return targetZipFile
53 }
54
55 fun File.deCompress(targetFileName: String): File {
56     return this.deCompress(Paths.get(targetFileName).toFile())
57 }
58
59 /**
60  * De-Compress the current zip file to the target file and return the target file
61  */
62 fun File.deCompress(targetFile: File): File {
63     BluePrintArchiveUtils.deCompress(this, targetFile.path)
64     return targetFile
65 }
66
67 fun deleteDir(path: String) {
68     Paths.get(path).toFile().deleteRecursively()
69 }
70
71
72 fun normalizedFile(path: String): File {
73     return Paths.get(path).toFile().normalize()
74 }