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 #include "base_extractor.h"
17
18 #include <fstream>
19 #include <dirent.h>
20
21 #include "string_ex.h"
22 #include "app_log_wrapper.h"
23 #include "bundle_constants.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27
BaseExtractor(const std::string & source)28 BaseExtractor::BaseExtractor(const std::string &source) : sourceFile_(source), zipFile_(source)
29 {
30 APP_LOGI("BaseExtractor instance is created");
31 }
32
~BaseExtractor()33 BaseExtractor::~BaseExtractor()
34 {
35 APP_LOGI("BaseExtractor instance is destroyed");
36 }
37
Init()38 bool BaseExtractor::Init()
39 {
40 if (!zipFile_.Open()) {
41 APP_LOGE("open zip file failed");
42 return false;
43 }
44 initial_ = true;
45 APP_LOGI("success");
46 return true;
47 }
48
ExtractByName(const std::string & fileName,std::ostream & dest) const49 bool BaseExtractor::ExtractByName(const std::string &fileName, std::ostream &dest) const
50 {
51 if (!initial_) {
52 APP_LOGE("extractor is not initial");
53 return false;
54 }
55 if (!zipFile_.ExtractFile(fileName, dest)) {
56 APP_LOGE("extractor is not ExtractFile");
57 return false;
58 }
59 return true;
60 }
61
ExtractFile(const std::string & fileName,const std::string & targetPath) const62 bool BaseExtractor::ExtractFile(const std::string &fileName, const std::string &targetPath) const
63 {
64 APP_LOGD("begin to extract %{public}s file into %{public}s targetPath", fileName.c_str(), targetPath.c_str());
65 std::ofstream fileStream;
66 fileStream.open(targetPath, std::ios_base::out | std::ios_base::binary);
67 if (!fileStream.is_open()) {
68 APP_LOGE("fail to open %{public}s file to write", targetPath.c_str());
69 return false;
70 }
71 if ((!ExtractByName(fileName, fileStream)) || (!fileStream.good())) {
72 APP_LOGE("fail to extract %{public}s zip file into stream", fileName.c_str());
73 fileStream.clear();
74 fileStream.close();
75 if (remove(targetPath.c_str()) != 0) {
76 APP_LOGE("fail to remove %{private}s file which writes stream error", targetPath.c_str());
77 }
78 return false;
79 }
80 fileStream.clear();
81 fileStream.close();
82 return true;
83 }
84
GetZipFileNames(std::vector<std::string> & fileNames)85 bool BaseExtractor::GetZipFileNames(std::vector<std::string> &fileNames)
86 {
87 auto &entryMap = zipFile_.GetAllEntries();
88 for (auto &entry : entryMap) {
89 fileNames.emplace_back(entry.first);
90 }
91 return true;
92 }
93
94 } // namespace AppExecFwk
95 } // namespace OHOS
96