summaryrefslogtreecommitdiffstats
path: root/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java
blob: d10a7b918c214c2965964d5c68962065b356fae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright 2017 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vnfsdk.functest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public final class FileUtil {

    public static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);

    private static final int BUFFER_SIZE = 2 * 1024 * 1024;

    private static final int TRY_COUNT = 3;

    private FileUtil() {

    }

    /**
     * Create directory.
     *
     * @param dir directory to create
     * @return boolean
     */
    public static boolean createDirectory(String dir) {
        File folder = new File(dir);
        int tryCount = 0;
        while (tryCount < TRY_COUNT) {
            tryCount++;
            if (!folder.exists() && !folder.mkdirs()) {
                continue;
            } else {
                return true;
            }
        }

        return folder.exists();
    }

    /**
     * delete file.
     *
     * @param file the file to delete
     * @return boolean
     */
    public static boolean deleteFile(File file) {
        String hintInfo = file.isDirectory() ? "dir " : "file ";
        boolean isFileDeleted = file.delete();
        boolean isFileExist = file.exists();
        if (!isFileExist) {
            if (isFileDeleted) {
                LOG.info("delete " + hintInfo + file.getAbsolutePath());
            } else {
                isFileDeleted = true;
                LOG.info("file not exist. no need delete " + hintInfo + file.getAbsolutePath());
            }
        } else {
            LOG.info("fail to delete " + hintInfo + file.getAbsolutePath());
        }
        return isFileDeleted;
    }

    /**
     * unzip zip file.
     *
     * @param zipFileName file name to zip
     * @param extPlace    extPlace
     * @return unzip file name
     * @throws IOException e1
     */
    public static List<String> unzip(String zipFileName, String extPlace) throws IOException {

        ArrayList<String> unzipFileNams = new ArrayList<String>();

        try (ZipFile zipFile = new ZipFile(zipFileName)) {
            Enumeration<?> fileEn = zipFile.entries();
            byte[] buffer = new byte[BUFFER_SIZE];

            while (fileEn.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) fileEn.nextElement();
                if (entry.isDirectory()) {
                    continue;
                }
                File file = new File(extPlace, entry.getName());
                if (!file.getParentFile().exists()) {
                    createDirectory(file.getParentFile().getAbsolutePath());
                }
                try (InputStream input = zipFile.getInputStream(entry);
                     BufferedOutputStream bos =  new BufferedOutputStream(new FileOutputStream(file))) {

                    while (true) {
                        int length = input.read(buffer);
                        if (length == -1) {
                            break;
                        }
                        bos.write(buffer, 0, length);
                    }
                    unzipFileNams.add(file.getAbsolutePath());
                } catch (Exception ex) {
                    LOG.error("close InputStream error!: " + ex);
                    LOG.error("close OutputStream error!: " + ex);
                }
            }
        } catch (Exception ex) {
            LOG.error("close ZipFile error!: " + ex);
            throw new IOException(ex);
        }
        return unzipFileNams;
    }

    public static String[] getDirectory(String directory) {
        File file = new File(directory);
        return file.list(new FilenameFilter() {

            public boolean accept(File current, String name) {
                return new File(current, name).isDirectory();
            }
        });
    }

    public static Boolean checkFileExist(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }

    public static Boolean deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            return file.delete();
        }
        return true;
    }

    public static byte[] convertZipFiletoByteArray(String filename) {
        File file = new File(filename);
        byte[] emptyArray = new byte[0];
        if (!file.exists()) {
            return emptyArray;
        }

        byte[] byteArrayFile = new byte[(int) file.length()];
        try (FileInputStream fileInputStream = new FileInputStream(filename)) {
            int value = fileInputStream.read(byteArrayFile);
            LOG.debug("Number of bytes read from fileInputStream = " + value);
        } catch (Exception e) {
            LOG.error("convertZipFiletoByteArray: " + e);
        }
        return byteArrayFile;
    }

    /**
     * Interface to Delete Directory
     * <br/>
     *
     * @param directory
     * @since VNFSDK 2.0
     */
    public static void deleteDirectory(String directory) {
        File file = new File(directory);
        if (!file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            for (File sub : file.listFiles()) {
                deleteFile(sub);
            }
        }
        file.delete();
    }
}