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 UTILITY_HIVIEW_ZIP_UTIL_H
17 #define UTILITY_HIVIEW_ZIP_UTIL_H
18 
19 #include <contrib/minizip/unzip.h>
20 #include <contrib/minizip/zip.h>
21 #include <string>
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 enum class ZipFileLevel {
26     KEEP_NONE_PARENT_PATH,
27     KEEP_ONE_PARENT_PATH
28 };
29 
30 class HiviewZipUnit {
31 public:
32     HiviewZipUnit(const std::string& zipPath, int32_t zipMode = APPEND_STATUS_CREATE);
33     ~HiviewZipUnit();
34 
isValid()35     bool isValid() const { return zipFile_ != nullptr; }
36     int32_t AddFileInZip(const std::string& srcFile, ZipFileLevel zipFileLevel);
37 
38 private:
39     std::string GetDstFilePath(const std::string& srcFile, ZipFileLevel zipFileLevel);
40     FILE* GetFileHandle(const std::string& file, std::string& realPath);
41 
42 private:
43     zipFile zipFile_ { nullptr };
44 };
45 
46 class HiviewUnzipUnit {
47 public:
48     HiviewUnzipUnit(const std::string& zipPath, const std::string& dstDir);
49     ~HiviewUnzipUnit();
50 
51     bool UnzipFile() const;
52 
53 private:
54     bool DoUnzipFile(const std::string& path, int& fileSize) const;
55 
56 private:
57     unzFile zipFile_ { nullptr };
58     std::string dstDir_;
59 };
60 } // namespace HiviewDFX
61 } // namespace OHOS
62 #endif // UTILITY_HIVIEW_ZIP_UTIL_H