CZipLoader.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * CZipLoader.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "ISimpleResourceLoader.h"
  12. #include "CInputStream.h"
  13. #include "ResourceID.h"
  14. #include "CCompressedStream.h"
  15. #include "MinizipExtensions.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class DLL_LINKAGE CZipStream : public CBufferedStream
  18. {
  19. unzFile file;
  20. public:
  21. /**
  22. * @brief constructs zip stream from already opened file
  23. * @param api virtual filesystem interface
  24. * @param archive path to archive to open
  25. * @param filepos position of file to open
  26. */
  27. CZipStream(const std::shared_ptr<CIOApi> & api, const boost::filesystem::path & archive, unz64_file_pos filepos);
  28. ~CZipStream();
  29. si64 getSize() override;
  30. ui32 calculateCRC32() override;
  31. protected:
  32. si64 readMore(ui8 * data, si64 size) override;
  33. };
  34. class DLL_LINKAGE CZipLoader : public ISimpleResourceLoader
  35. {
  36. std::shared_ptr<CIOApi> ioApi;
  37. zlib_filefunc64_def zlibApi;
  38. boost::filesystem::path archiveName;
  39. std::string mountPoint;
  40. std::unordered_map<ResourceID, unz64_file_pos> files;
  41. std::unordered_map<ResourceID, unz64_file_pos> listFiles(const std::string & mountPoint, const boost::filesystem::path &archive);
  42. public:
  43. CZipLoader(const std::string & mountPoint, const boost::filesystem::path & archive, std::shared_ptr<CIOApi> api = std::shared_ptr<CIOApi>(new CDefaultIOApi()));
  44. /// Interface implementation
  45. /// @see ISimpleResourceLoader
  46. std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const override;
  47. bool existsResource(const ResourceID & resourceName) const override;
  48. std::string getMountPoint() const override;
  49. void updateFilteredFiles(std::function<bool(const std::string &)> filter) const override {}
  50. std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const override;
  51. };
  52. namespace ZipArchive
  53. {
  54. /// List all files present in archive
  55. std::vector<std::string> DLL_LINKAGE listFiles(const boost::filesystem::path & filename);
  56. /// extracts all files from archive "from" into destination directory "where". Directory must exist
  57. bool DLL_LINKAGE extract(const boost::filesystem::path & from, const boost::filesystem::path & where);
  58. ///same as above, but extracts only files mentioned in "what" list
  59. bool DLL_LINKAGE extract(const boost::filesystem::path & from, const boost::filesystem::path & where, const std::vector<std::string> & what);
  60. }
  61. VCMI_LIB_NAMESPACE_END