#pragma once #include "coding/file_reader.hpp" #include "coding/file_writer.hpp" #include "base/exception.hpp" #include #include #include #include #include #include #include class ZipFileReader : public FileReader { public: struct Delegate { virtual ~Delegate() = default; // When |size| is zero, end of file is reached. virtual void OnBlockUnzipped(size_t size, char const * data) = 0; virtual void OnStarted() {} virtual void OnCompleted() {} }; using ProgressFn = std::function; /// Contains file name inside zip and it's uncompressed size using FileList = std::vector>; DECLARE_EXCEPTION(OpenZipException, OpenException); DECLARE_EXCEPTION(LocateZipException, OpenException); DECLARE_EXCEPTION(InvalidZipException, OpenException); ZipFileReader(std::string const & container, std::string const & file, uint32_t logPageSize = FileReader::kDefaultLogPageSize, uint32_t logPageCount = FileReader::kDefaultLogPageCount); /// @note Size() returns compressed file size inside zip uint64_t UncompressedSize() const { return m_uncompressedFileSize; } /// @returns true for a deflated entry, false for a stored (method 0, readable in place) one. bool IsCompressed() const { return m_isCompressed; } /// @warning Can also throw Writer::OpenException and Writer::WriteException static void UnzipFile(std::string const & zipContainer, std::string const & fileInZip, Delegate & delegate); static void UnzipFile(std::string const & zipContainer, std::string const & fileInZip, std::string const & outPath); static void FilesList(std::string const & zipContainer, FileList & filesList); /// Quick version without exceptions static bool IsZip(std::string const & zipContainer); /// Opens |fileInZip| inside |zipContainer| and returns a random-access reader for it. /// A stored (uncompressed) entry is read in place from the container; a deflated entry is inflated /// once into memory (our in-place FileReader can only read uncompressed data from the container). static std::unique_ptr CreateModelReader(std::string const & zipContainer, std::string const & fileInZip, uint32_t logPageSize = FileReader::kDefaultLogPageSize, uint32_t logPageCount = FileReader::kDefaultLogPageCount); private: uint64_t m_uncompressedFileSize; bool m_isCompressed; };