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
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.
16 * ============LICENSE_END========================================================================
19 package com.oransc.rappmanager;
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;
32 public class WriteCsarFile {
34 public static void main(String[] args) {
36 "rapp-manager-application\\src\\main\\resources\\resource-csar\\rapp.csar";
38 "rapp-manager-application\\src\\main\\resources\\resource-csar";
41 FileOutputStream fos = new FileOutputStream(zipFileName);
42 ZipOutputStream zos = new ZipOutputStream(fos);
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());
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());
59 ZipEntry zipEntry = new ZipEntry(file.getPath().replaceAll("\\\\", "/"));
60 zos.putNextEntry(zipEntry);
62 byte[] bytes = new byte[1024];
64 while ((length = fis.read(bytes)) >= 0) {
65 zos.write(bytes, 0, length);
69 System.out.println("Not a file: " + file.getPath());
76 System.out.println("Zip file created successfully!");
78 } catch (IOException e) {
83 private static void getFiles(File baseDirectory, File directory, List<File> fileList) {
84 File[] files = directory.listFiles();
86 for (File file : files) {
87 if (file.isDirectory()) {
88 getFiles(baseDirectory, file, fileList);
90 fileList.add(new File(file.getPath().replace(baseDirectory.getPath() + File.separator, "")));