• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ZipFileInfo()49     const zip_fileinfo& ZipFileInfo() const
50     {
51         return zipFileInfo_;
52     }
53 
IsOpen()54     bool IsOpen() const
55     {
56         return (zipFile_ != nullptr);
57     }
58 
59     int32_t AddFileOrDirectoryToZip(const std::string &filePath, const std::string &zipPath);
60     int32_t AddFileOrDirectoryToZip(const fs::path &fsFilePath, const fs::path &fsZipPath);
61 
62     int32_t WriteStringToZip(const std::string &content, const std::string &zipPath);
63 
64 protected:
65     int32_t AddEmptyDirToZip(const std::string &zipPath);
66     int32_t AddEmptyDirToZip(const fs::path &fsZipPath);
67     int32_t AddFileToZip(const std::string &filePath, const std::string &zipPath);
68     int32_t AddFileToZip(const fs::path &fsFilePath, const fs::path &fsZipPath);
69 
70 private:
71     zipFile zipFile_ = nullptr;
72     zip_fileinfo zipFileInfo_ = {};
73     std::string zipFilePath_;
74     ZipLevel zipLevel_ = ZipLevel::ZIP_LEVEL_DEFAULT;
75 };
76 }  // namespace AppPackingTool
77 }  // namespace OHOS
78 #endif  // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_ZIP_WRAPPER_H
79