1 /* 2 * Copyright (c) 2024 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 #ifndef DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_WRAPPER_H 17 #define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_WRAPPER_H 18 19 #include <filesystem> 20 #include <string> 21 22 #include "zip_constants.h" 23 24 namespace OHOS { 25 namespace AppPackingTool { 26 class ZipWrapper { 27 public: 28 ZipWrapper(); 29 explicit ZipWrapper(std::string zipPath); 30 virtual ~ZipWrapper(); 31 32 ZipWrapper(const ZipWrapper &) = delete; 33 ZipWrapper &operator=(const ZipWrapper &) = delete; 34 35 int32_t Open(std::string& zipPath, int32_t append = APPEND_STATUS_CREATE); 36 int32_t Open(int32_t append = APPEND_STATUS_CREATE); 37 void Close(); 38 SetZipFilePath(std::string path)39 void SetZipFilePath(std::string path) 40 { 41 zipFilePath_ = path; 42 } 43 SetZipLevel(ZipLevel zipLevel)44 void SetZipLevel(ZipLevel zipLevel) 45 { 46 zipLevel_ = zipLevel; 47 } 48 SetZipMethod(ZipMethod zipMethod)49 void SetZipMethod(ZipMethod zipMethod) 50 { 51 zipMethod_ = zipMethod; 52 } 53 ZipFileInfo()54 const zip_fileinfo& ZipFileInfo() const 55 { 56 return zipFileInfo_; 57 } 58 IsOpen()59 bool IsOpen() const 60 { 61 return (zipFile_ != nullptr); 62 } 63 64 int32_t AddFileOrDirectoryToZip(const std::string &filePath, const std::string &zipPath); 65 int32_t AddFileOrDirectoryToZip(const fs::path &fsFilePath, const fs::path &fsZipPath); 66 67 int32_t WriteStringToZip(const std::string &content, const std::string &zipPath); 68 69 protected: 70 int32_t AddEmptyDirToZip(const std::string &zipPath); 71 int32_t AddEmptyDirToZip(const fs::path &fsZipPath); 72 int32_t AddFileToZip(const std::string &filePath, const std::string &zipPath); 73 int32_t AddFileToZip(const fs::path &fsFilePath, const fs::path &fsZipPath); 74 75 private: 76 zipFile zipFile_ = nullptr; 77 zip_fileinfo zipFileInfo_ = {}; 78 std::string zipFilePath_; 79 ZipLevel zipLevel_ = ZipLevel::ZIP_LEVEL_DEFAULT; 80 ZipMethod zipMethod_ = ZipMethod::ZIP_METHOD_STORED; 81 }; 82 } // namespace AppPackingTool 83 } // namespace OHOS 84 #endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_WRAPPER_H 85