100600bfbSopenharmony_ci/* 200600bfbSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400600bfbSopenharmony_ci * you may not use this file except in compliance with the License. 500600bfbSopenharmony_ci * You may obtain a copy of the License at 600600bfbSopenharmony_ci * 700600bfbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800600bfbSopenharmony_ci * 900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and 1300600bfbSopenharmony_ci * limitations under the License. 1400600bfbSopenharmony_ci */ 1500600bfbSopenharmony_ci#include "util/zip_utils.h" 1600600bfbSopenharmony_ci#include "directory_ex.h" 1700600bfbSopenharmony_ci#include "dump_utils.h" 1800600bfbSopenharmony_ci#include "util/zip/zip_writer.h" 1900600bfbSopenharmony_ci#include "hilog_wrapper.h" 2000600bfbSopenharmony_cinamespace OHOS { 2100600bfbSopenharmony_cinamespace HiviewDFX { 2200600bfbSopenharmony_cibool ZipUtils::ZipFolder(const std::string &srcPath, const std::string &dstFile, const ZipTickNotify notify) 2300600bfbSopenharmony_ci{ 2400600bfbSopenharmony_ci DUMPER_HILOGD(MODULE_COMMON, "enter|srcPath=[%{public}s], dstFile=[%{public}s]", 2500600bfbSopenharmony_ci srcPath.c_str(), dstFile.c_str()); 2600600bfbSopenharmony_ci 2700600bfbSopenharmony_ci std::string srcFolder = IncludeTrailingPathDelimiter(srcPath); 2800600bfbSopenharmony_ci 2900600bfbSopenharmony_ci DUMPER_HILOGD(MODULE_COMMON, "debug|srcFolder=[%{public}s]", srcFolder.c_str()); 3000600bfbSopenharmony_ci 3100600bfbSopenharmony_ci if (!DumpUtils::DirectoryExists(srcFolder)) { 3200600bfbSopenharmony_ci DUMPER_HILOGE(MODULE_COMMON, "leave|ret=false, srcFolder=[%{public}s]", srcFolder.c_str()); 3300600bfbSopenharmony_ci return false; 3400600bfbSopenharmony_ci } 3500600bfbSopenharmony_ci 3600600bfbSopenharmony_ci DUMPER_HILOGD(MODULE_COMMON, "debug|GetDirFiles, srcFolder=[%{public}s]", srcFolder.c_str()); 3700600bfbSopenharmony_ci 3800600bfbSopenharmony_ci std::vector<std::string> allFiles; 3900600bfbSopenharmony_ci GetDirFiles(srcFolder, allFiles); 4000600bfbSopenharmony_ci 4100600bfbSopenharmony_ci if ((notify != nullptr) && (notify(UNSET_PROGRESS, UNSET_PROGRESS))) { 4200600bfbSopenharmony_ci DUMPER_HILOGE(MODULE_COMMON, "leave|notify"); 4300600bfbSopenharmony_ci return false; 4400600bfbSopenharmony_ci } 4500600bfbSopenharmony_ci 4600600bfbSopenharmony_ci for (auto str : allFiles) { 4700600bfbSopenharmony_ci DUMPER_HILOGD(MODULE_COMMON, "debug|str=[%{public}s]", str.c_str()); 4800600bfbSopenharmony_ci } 4900600bfbSopenharmony_ci 5000600bfbSopenharmony_ci size_t zipRootLen = srcFolder.length(); 5100600bfbSopenharmony_ci std::vector<std::pair<std::string, std::string>> zipItems; 5200600bfbSopenharmony_ci std::transform(allFiles.begin(), allFiles.end(), std::back_inserter(zipItems), 5300600bfbSopenharmony_ci [zipRootLen](const std::string &str) { 5400600bfbSopenharmony_ci return std::make_pair(str, str.substr(zipRootLen)); // first:absolutePath, second:relativePath 5500600bfbSopenharmony_ci }); 5600600bfbSopenharmony_ci allFiles.clear(); 5700600bfbSopenharmony_ci 5800600bfbSopenharmony_ci for (auto zipItem : zipItems) { 5900600bfbSopenharmony_ci DUMPER_HILOGD(MODULE_COMMON, "debug|zipItems, absPath=[%{public}s], relPath=[%{public}s]", 6000600bfbSopenharmony_ci zipItem.first.c_str(), zipItem.second.c_str()); 6100600bfbSopenharmony_ci } 6200600bfbSopenharmony_ci 6300600bfbSopenharmony_ci DUMPER_HILOGD(MODULE_COMMON, "debug|Create, dstFile=[%{public}s]", dstFile.c_str()); 6400600bfbSopenharmony_ci 6500600bfbSopenharmony_ci ZipWriter zipWriter(dstFile); 6600600bfbSopenharmony_ci zipWriter.Open(); 6700600bfbSopenharmony_ci bool ret = zipWriter.Write(zipItems, notify); 6800600bfbSopenharmony_ci 6900600bfbSopenharmony_ci DUMPER_HILOGD(MODULE_COMMON, "leave|ret=%{public}d", ret); 7000600bfbSopenharmony_ci return ret; 7100600bfbSopenharmony_ci} 7200600bfbSopenharmony_ci} // namespace HiviewDFX 7300600bfbSopenharmony_ci} // namespace OHOS 74