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#include "util/zip_utils.h" 16#include "directory_ex.h" 17#include "dump_utils.h" 18#include "util/zip/zip_writer.h" 19#include "hilog_wrapper.h" 20namespace OHOS { 21namespace HiviewDFX { 22bool ZipUtils::ZipFolder(const std::string &srcPath, const std::string &dstFile, const ZipTickNotify notify) 23{ 24 DUMPER_HILOGD(MODULE_COMMON, "enter|srcPath=[%{public}s], dstFile=[%{public}s]", 25 srcPath.c_str(), dstFile.c_str()); 26 27 std::string srcFolder = IncludeTrailingPathDelimiter(srcPath); 28 29 DUMPER_HILOGD(MODULE_COMMON, "debug|srcFolder=[%{public}s]", srcFolder.c_str()); 30 31 if (!DumpUtils::DirectoryExists(srcFolder)) { 32 DUMPER_HILOGE(MODULE_COMMON, "leave|ret=false, srcFolder=[%{public}s]", srcFolder.c_str()); 33 return false; 34 } 35 36 DUMPER_HILOGD(MODULE_COMMON, "debug|GetDirFiles, srcFolder=[%{public}s]", srcFolder.c_str()); 37 38 std::vector<std::string> allFiles; 39 GetDirFiles(srcFolder, allFiles); 40 41 if ((notify != nullptr) && (notify(UNSET_PROGRESS, UNSET_PROGRESS))) { 42 DUMPER_HILOGE(MODULE_COMMON, "leave|notify"); 43 return false; 44 } 45 46 for (auto str : allFiles) { 47 DUMPER_HILOGD(MODULE_COMMON, "debug|str=[%{public}s]", str.c_str()); 48 } 49 50 size_t zipRootLen = srcFolder.length(); 51 std::vector<std::pair<std::string, std::string>> zipItems; 52 std::transform(allFiles.begin(), allFiles.end(), std::back_inserter(zipItems), 53 [zipRootLen](const std::string &str) { 54 return std::make_pair(str, str.substr(zipRootLen)); // first:absolutePath, second:relativePath 55 }); 56 allFiles.clear(); 57 58 for (auto zipItem : zipItems) { 59 DUMPER_HILOGD(MODULE_COMMON, "debug|zipItems, absPath=[%{public}s], relPath=[%{public}s]", 60 zipItem.first.c_str(), zipItem.second.c_str()); 61 } 62 63 DUMPER_HILOGD(MODULE_COMMON, "debug|Create, dstFile=[%{public}s]", dstFile.c_str()); 64 65 ZipWriter zipWriter(dstFile); 66 zipWriter.Open(); 67 bool ret = zipWriter.Write(zipItems, notify); 68 69 DUMPER_HILOGD(MODULE_COMMON, "leave|ret=%{public}d", ret); 70 return ret; 71} 72} // namespace HiviewDFX 73} // namespace OHOS 74