• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.datasources.utils.common.util;
17 
18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
19 import ohos.devtools.datasources.utils.session.service.SessionManager;
20 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
21 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 
25 import java.io.Closeable;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Objects;
33 
34 /**
35  * FileUtils
36  *
37  * @since 2021/08/06 15:22
38  */
39 public final class FileUtils {
40     private static final Logger LOGGER = LogManager.getLogger(FileUtils.class);
41 
42     private static final String TMP_DIR = SessionManager.getInstance().tempPath();
43 
FileUtils()44     private FileUtils() {
45     }
46 
47     /**
48      * unzipTarFile
49      *
50      * @param file file
51      * @return List<String>
52      */
unzipTarFile(File file)53     public static List<String> unzipTarFile(File file) {
54         List<String> fileNames = new ArrayList<>();
55         if (Objects.nonNull(file)) {
56             TarArchiveInputStream tarArchiveInputStream = null;
57             FileOutputStream fileOutputStream = null;
58             try {
59                 tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(file));
60                 while (true) {
61                     TarArchiveEntry nextTarEntry = tarArchiveInputStream.getNextTarEntry();
62                     if (Objects.isNull(nextTarEntry)) {
63                         break;
64                     }
65                     fileNames.add(nextTarEntry.getName());
66                     if (nextTarEntry.isDirectory()) {
67                         new File(TMP_DIR + nextTarEntry.getName()).mkdirs();
68                     } else {
69                         File pluginFile = new File(TMP_DIR + nextTarEntry.getName());
70                         if (!pluginFile.getParentFile().exists()) {
71                             pluginFile.getParentFile().mkdirs();
72                         }
73                         if (!pluginFile.exists()) {
74                             pluginFile.createNewFile();
75                         }
76                         fileOutputStream = new FileOutputStream(pluginFile);
77                         byte[] bs = new byte[1024];
78                         int len = -1;
79                         while ((len = tarArchiveInputStream.read(bs)) != -1) {
80                             fileOutputStream.write(bs, 0, len);
81                         }
82                         fileOutputStream.flush();
83                     }
84                 }
85             } catch (IOException ioException) {
86                 if (ProfilerLogManager.isErrorEnabled()) {
87                     LOGGER.error("uzipFile Exception ", ioException);
88                 }
89             } finally {
90                 closeStream(tarArchiveInputStream);
91                 closeStream(fileOutputStream);
92             }
93         }
94         return fileNames;
95     }
96 
closeStream(Closeable closeable)97     private static void closeStream(Closeable closeable) {
98         if (closeable != null) {
99             try {
100                 closeable.close();
101             } catch (IOException ioException) {
102                 if (ProfilerLogManager.isErrorEnabled()) {
103                     LOGGER.error("uzipFile Exception ", ioException);
104                 }
105             }
106         }
107     }
108 }
109