Add Rapp Instances
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / main / java / com / oransc / rappmanager / WriteCsarFile.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.zip.ZipEntry;
30 import java.util.zip.ZipOutputStream;
31
32 public class WriteCsarFile {
33
34     public static void main(String[] args) {
35         String zipFileName =
36                 "rapp-manager-application\\src\\main\\resources\\resource-csar\\rapp.csar";
37         String csarPath =
38                 "rapp-manager-application\\src\\main\\resources\\resource-csar";
39
40         try {
41             FileOutputStream fos = new FileOutputStream(zipFileName);
42             ZipOutputStream zos = new ZipOutputStream(fos);
43
44             File directory = new File(csarPath);
45             System.out.println("Is directory " + directory.isDirectory());
46             List<File> fileList = new ArrayList<>();
47             getFiles(directory, directory, fileList);
48             File[] files = fileList.toArray(new File[0]);
49             System.out.println("File size :" + files.length);
50             Arrays.sort(files, Collections.reverseOrder());
51
52
53             for (File file : files) {
54                 System.out.println("Processing " + file.getPath());
55                 if (!file.isDirectory()) {
56                     System.out.println("Processing " + file.getPath());
57                     FileInputStream fis = new FileInputStream(csarPath + File.separator + file.getPath());
58
59                     ZipEntry zipEntry = new ZipEntry(file.getPath().replaceAll("\\\\", "/"));
60                     zos.putNextEntry(zipEntry);
61
62                     byte[] bytes = new byte[1024];
63                     int length;
64                     while ((length = fis.read(bytes)) >= 0) {
65                         zos.write(bytes, 0, length);
66                     }
67                     fis.close();
68                 } else {
69                     System.out.println("Not a file: " + file.getPath());
70                 }
71             }
72
73             zos.close();
74             fos.close();
75
76             System.out.println("Zip file created successfully!");
77
78         } catch (IOException e) {
79             e.printStackTrace();
80         }
81     }
82
83     private static void getFiles(File baseDirectory, File directory, List<File> fileList) {
84         File[] files = directory.listFiles();
85         if (files != null) {
86             for (File file : files) {
87                 if (file.isDirectory()) {
88                     getFiles(baseDirectory, file, fileList);
89                 } else {
90                     fileList.add(new File(file.getPath().replace(baseDirectory.getPath() + File.separator, "")));
91                 }
92             }
93         }
94     }
95 }