CZipLoader.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. /*
  3. * CZipLoader.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. #include "ISimpleResourceLoader.h"
  12. #include "CInputStream.h"
  13. #include "ResourceID.h"
  14. #include "CCompressedStream.h"
  15. // Necessary here in order to get all types
  16. #ifdef USE_SYSTEM_MINIZIP
  17. #include <minizip/unzip.h>
  18. #else
  19. #include "../minizip/unzip.h"
  20. #endif
  21. class DLL_LINKAGE CZipStream : public CBufferedStream
  22. {
  23. unzFile file;
  24. public:
  25. /**
  26. * @brief constructs zip stream from already opened file
  27. * @param archive path to archive to open
  28. * @param filepos position of file to open
  29. */
  30. CZipStream(const boost::filesystem::path & archive, unz64_file_pos filepos);
  31. ~CZipStream();
  32. si64 getSize() override;
  33. ui32 calculateCRC32() override;
  34. protected:
  35. si64 readMore(ui8 * data, si64 size) override;
  36. };
  37. class DLL_LINKAGE CZipLoader : public ISimpleResourceLoader
  38. {
  39. boost::filesystem::path archiveName;
  40. std::string mountPoint;
  41. std::unordered_map<ResourceID, unz64_file_pos> files;
  42. std::unordered_map<ResourceID, unz64_file_pos> listFiles(const std::string & mountPoint, const boost::filesystem::path &archive);
  43. public:
  44. CZipLoader(const std::string & mountPoint, const boost::filesystem::path & archive);
  45. /// Interface implementation
  46. /// @see ISimpleResourceLoader
  47. std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const override;
  48. bool existsResource(const ResourceID & resourceName) const override;
  49. std::string getMountPoint() 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(boost::filesystem::path filename);
  56. /// extracts all files from archive "from" into destination directory "where". Directory must exist
  57. bool DLL_LINKAGE extract(boost::filesystem::path from, boost::filesystem::path where);
  58. ///same as above, but extracts only files mentioned in "what" list
  59. bool DLL_LINKAGE extract(boost::filesystem::path from, boost::filesystem::path where, std::vector<std::string> what);
  60. }