CZipLoader.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "../minizip/unzip.h"
  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 archive path to archive to open
  24. * @param filepos position of file to open
  25. */
  26. CZipStream(const std::string & archive, unz_file_pos filepos);
  27. ~CZipStream();
  28. si64 getSize() override;
  29. ui32 calculateCRC32() override;
  30. protected:
  31. si64 readMore(ui8 * data, si64 size) override;
  32. };
  33. class DLL_LINKAGE CZipLoader : public ISimpleResourceLoader
  34. {
  35. std::string archiveName;
  36. std::string mountPoint;
  37. std::unordered_map<ResourceID, unz_file_pos> files;
  38. std::unordered_map<ResourceID, unz_file_pos> listFiles(const std::string & mountPoint, const std::string &archive);
  39. public:
  40. CZipLoader(const std::string & mountPoint, const std::string & archive);
  41. /// Interface implementation
  42. /// @see ISimpleResourceLoader
  43. std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const override;
  44. bool existsResource(const ResourceID & resourceName) const override;
  45. std::string getMountPoint() const override;
  46. std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const override;
  47. };
  48. namespace ZipArchive
  49. {
  50. /// List all files present in archive
  51. std::vector<std::string> DLL_LINKAGE listFiles(std::string filename);
  52. /// extracts all files from archive "from" into destination directory "where". Directory must exist
  53. bool DLL_LINKAGE extract(std::string from, std::string where);
  54. ///same as above, but extracts only files mentioned in "what" list
  55. bool DLL_LINKAGE extract(std::string from, std::string where, std::vector<std::string> what);
  56. }