1 /* 2 * Copyright (c) 2023 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 OHOS_ABILITY_BASE_ZIP_FILE_READER_H 17 #define OHOS_ABILITY_BASE_ZIP_FILE_READER_H 18 19 #include <fcntl.h> 20 #include <memory> 21 #include <unistd.h> 22 #include <string> 23 24 namespace OHOS { 25 namespace AbilityBase { 26 class ZipFileReader { 27 public: 28 static std::shared_ptr<ZipFileReader> CreateZipFileReader(const std::string &filePath); 29 static size_t GetFileLen(const std::string &filePath); 30 ZipFileReader(const std::string & filePath)31 ZipFileReader(const std::string &filePath) : filePath_(filePath) {} 32 ZipFileReader(ZipFileReader &) = delete; 33 void operator=(ZipFileReader &) = delete; 34 virtual ~ZipFileReader(); 35 36 virtual std::string ReadBuffer(size_t startPos, size_t bufferSize) = 0; 37 virtual bool ReadBuffer(uint8_t *dst, size_t startPos, size_t bufferSize) = 0; GetFileLen()38 size_t GetFileLen() const 39 { 40 return fileLen_; 41 } GetFd()42 int GetFd() const 43 { 44 return fd_; 45 } SetClosable(bool closable)46 void SetClosable(bool closable) 47 { 48 closable_ = closable; 49 } 50 protected: 51 virtual bool init(); 52 53 protected: 54 std::string filePath_; 55 size_t fileLen_ = 0; 56 57 // For safe memory, reserve this field and keep the file opened. 58 int fd_ = -1; 59 // close fd in destructor 60 bool closable_ = true; 61 }; 62 } 63 } 64 65 #endif