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 FOUNDATION_AAFWK_STANDARD_TOOLS_ZIP_WRITER_H 16 #define FOUNDATION_AAFWK_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 AAFwk { 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 std::unique_ptr<ZipWriter> CreateWithFd(PlatformFile zipFilefd, const FilePath &rootDir); 39 static std::unique_ptr<ZipWriter> Create(const FilePath &zip_file, const FilePath &rootDir); 40 ~ZipWriter(); 41 42 // Writes the files at |paths| to the ZIP file and closes this Zip file. 43 // Note that the the FilePaths must be relative to |rootDir| specified in the 44 // Create method. 45 // Returns true if all entries were written successfuly. 46 bool WriteEntries(const std::vector<FilePath> &paths, const OPTIONS &options, CALLBACK callback); 47 48 private: 49 ZipWriter(zipFile zip_file, const FilePath &rootDir); 50 51 // Writes the pending entries to the ZIP file if there are at least 52 // |kMaxPendingEntriesCount| 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, CALLBACK callback); 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<FilePath> &paths, const OPTIONS &options, CALLBACK callback); 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, CALLBACK callback); 66 67 // The entries that have been added but not yet written to the ZIP file. 68 std::vector<FilePath> pendingEntries_; 69 70 // The actual zip file. 71 zipFile zipFile_; 72 73 // Path to the directory entry paths are relative to. 74 FilePath rootDir_; 75 76 DISALLOW_COPY_AND_ASSIGN(ZipWriter); 77 }; 78 } // namespace LIBZIP 79 } // namespace AAFwk 80 } // namespace OHOS 81 82 #endif // FOUNDATION_AAFWK_STANDARD_TOOLS_ZIP_WRITER_H