1 /* 2 * Copyright (c) 2022 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 FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_WRITER_H 16 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_WRITER_H 17 18 #include <memory> 19 #include <vector> 20 #include "file_path.h" 21 #include "zip_utils.h" 22 #include "contrib/minizip/unzip.h" 23 #include "contrib/minizip/zip.h" 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 namespace LIBZIP { 28 // A class used to write entries to a ZIP file and buffering the reading of 29 // files to limit the number of calls to the FileAccessor. This is for 30 // performance reasons as these calls may be expensive when IPC based). 31 // This class is so far internal and only used by zip.cpp, but could be made 32 // public if needed. 33 class ZipWriter { 34 public: 35 // Creates a writer that will write a ZIP file to |zipFilefd|/|zip_file| 36 // and which entries (specifies with AddEntries) are relative to |rootDir|. 37 // All file reads are performed using |file_accessor|. 38 static zipFile InitZipFileWithFd(PlatformFile zipFilefd); 39 static zipFile InitZipFileWithFile(const FilePath &zip_file_path); 40 ~ZipWriter(); 41 explicit ZipWriter(zipFile zip_file); 42 43 // Writes the files at |paths| to the ZIP file and closes this Zip file. 44 // Note that the the FilePaths must be relative to |rootDir| specified in the 45 // Create method. 46 // Returns true if all entries were written successfuly. 47 bool WriteEntries(const std::vector<std::pair<FilePath, FilePath>> &paths, const OPTIONS &options); 48 49 private: 50 51 // Writes the pending entries to the ZIP file if there are at least 52 // |g_MaxPendingEntriesCount| of them. If |force| is true, all pending entries 53 // are written regardless of how many there are. 54 // Returns false if writing an entry fails, true if no entry was written or 55 // there was no error writing entries. 56 bool FlushEntriesIfNeeded(bool force, const OPTIONS &options); 57 58 // Adds the files at |paths| to the ZIP file. These FilePaths must be relative 59 // to |rootDir| specified in the Create method. 60 bool AddEntries(const std::vector<std::pair<FilePath, FilePath>> &paths, const OPTIONS &options); 61 62 // Closes the ZIP file. 63 // Returns true if successful, false otherwise (typically if an entry failed 64 // to be written). 65 bool Close(const OPTIONS &options); 66 67 // The entries that have been added but not yet written to the ZIP file. 68 std::vector<std::pair<FilePath, FilePath>> pendingEntries_; 69 70 // The actual zip file. 71 zipFile zipFile_; 72 73 DISALLOW_COPY_AND_ASSIGN(ZipWriter); 74 }; 75 } // namespace LIBZIP 76 } // namespace AppExecFwk 77 } // namespace OHOS 78 79 #endif // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_WRITER_H