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 #ifndef HIDUMPER_UTIL_ZIP_WRITER_H
16 #define HIDUMPER_UTIL_ZIP_WRITER_H
17 #include <string>
18 #include <vector>
19 #include "contrib/minizip/zip.h"
20 #include "util/zip/zip_common_type.h"
21 #ifdef DUMP_TEST_MODE // for mock test
22 #include "gtest/gtest_prod.h"
23 #endif // for mock test
24 namespace OHOS {
25 namespace HiviewDFX {
26 class ZipWriter {
27 public:
28     ZipWriter(const std::string &zipFilePath);
29     ~ZipWriter();
30 public:
31     bool Open();
32     bool Close();
33     // zipItems: first:absolutePath, second:relativePath
34     bool Write(const std::vector<std::pair<std::string, std::string>> &zipItems,
35         const ZipTickNotify notify = nullptr);
36 private:
37     bool FlushItems(const ZipTickNotify notify = nullptr);
38     static bool SetTimeToZipFileInfo(zip_fileinfo &zipInfo);
39     static zipFile OpenForZipping(const std::string &fileName, int append);
40     static bool ZipOpenNewFileInZip(zipFile zip_file, const std::string &strPath);
41     static bool AddFileContentToZip(zipFile zip_file, std::string &file_path);
42     static bool OpenNewFileEntry(zipFile zip_file, std::string &path);
43     static bool CloseNewFileEntry(zipFile zip_file);
44     static bool AddFileEntryToZip(zipFile zip_file, std::string &relativePath, std::string &absolutePath);
45 private:
46     std::vector<std::pair<std::string, std::string>> zipItems_;
47     std::string zipFilePath_;
48     zipFile zipFile_;
49 #ifdef DUMP_TEST_MODE // for mock test
50     FRIEND_TEST(HidumperConfigUtilsTest, HidumperZipWriter001);
51 #endif // for mock test
52 };
53 } // namespace HiviewDFX
54 } // namespace OHOS
55 #endif // HIDUMPER_UTIL_ZIP_WRITER_H
56