2
0

CZipLoader.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include "MinizipExtensions.h"
  16. class DLL_LINKAGE CZipStream : public CBufferedStream
  17. {
  18. unzFile file;
  19. public:
  20. /**
  21. * @brief constructs zip stream from already opened file
  22. * @param api virtual filesystem interface
  23. * @param archive path to archive to open
  24. * @param filepos position of file to open
  25. */
  26. CZipStream(std::shared_ptr<CIOApi> api, 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::shared_ptr<CIOApi> ioApi;
  36. zlib_filefunc64_def zlibApi;
  37. std::string archiveName;
  38. std::string mountPoint;
  39. std::unordered_map<ResourceID, unz_file_pos> files;
  40. std::unordered_map<ResourceID, unz_file_pos> listFiles(const std::string & mountPoint, const std::string &archive);
  41. public:
  42. CZipLoader(const std::string & mountPoint, const std::string & archive, std::shared_ptr<CIOApi> api = std::shared_ptr<CIOApi>(new CDefaultIOApi()));
  43. /// Interface implementation
  44. /// @see ISimpleResourceLoader
  45. std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const override;
  46. bool existsResource(const ResourceID & resourceName) const override;
  47. std::string getMountPoint() const override;
  48. std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const override;
  49. };
  50. namespace ZipArchive
  51. {
  52. /// List all files present in archive
  53. std::vector<std::string> DLL_LINKAGE listFiles(std::string filename);
  54. /// extracts all files from archive "from" into destination directory "where". Directory must exist
  55. bool DLL_LINKAGE extract(std::string from, std::string where);
  56. ///same as above, but extracts only files mentioned in "what" list
  57. bool DLL_LINKAGE extract(std::string from, std::string where, std::vector<std::string> what);
  58. }