CZipLoader.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "ResourcePath.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<ResourcePath, unz64_file_pos> files;
  41. std::unordered_map<ResourcePath, 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 ResourcePath & resourceName) const override;
  47. bool existsResource(const ResourcePath & 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<ResourcePath> getFilteredFiles(std::function<bool(const ResourcePath &)> filter) const override;
  51. };
  52. class DLL_LINKAGE ZipArchive : boost::noncopyable
  53. {
  54. unzFile archive;
  55. public:
  56. ZipArchive(const boost::filesystem::path & from);
  57. ~ZipArchive();
  58. std::vector<std::string> listFiles();
  59. bool extract(const boost::filesystem::path & where, const std::vector<std::string> & what);
  60. bool extract(const boost::filesystem::path & where, const std::string & what);
  61. };
  62. VCMI_LIB_NAMESPACE_END