CZipLoader.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "StdInc.h"
  2. #include "../../Global.h"
  3. #include "CZipLoader.h"
  4. #include "../ScopeGuard.h"
  5. /*
  6. * CZipLoader.cpp, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. CZipStream::CZipStream(std::shared_ptr<CIOApi> api, const std::string & archive, unz_file_pos filepos)
  15. {
  16. zlib_filefunc64_def zlibApi;
  17. zlibApi = api->getApiStructure();
  18. file = unzOpen2_64(archive.c_str(), &zlibApi);
  19. unzGoToFilePos(file, &filepos);
  20. unzOpenCurrentFile(file);
  21. }
  22. CZipStream::~CZipStream()
  23. {
  24. unzCloseCurrentFile(file);
  25. unzClose(file);
  26. }
  27. si64 CZipStream::readMore(ui8 * data, si64 size)
  28. {
  29. return unzReadCurrentFile(file, data, size);
  30. }
  31. si64 CZipStream::getSize()
  32. {
  33. unz_file_info info;
  34. unzGetCurrentFileInfo (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  35. return info.uncompressed_size;
  36. }
  37. ui32 CZipStream::calculateCRC32()
  38. {
  39. unz_file_info info;
  40. unzGetCurrentFileInfo (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  41. return info.crc;
  42. }
  43. CZipLoader::CZipLoader(const std::string & mountPoint, const std::string & archive, std::shared_ptr<CIOApi> api):
  44. ioApi(api),
  45. zlibApi(ioApi->getApiStructure()),
  46. archiveName(archive),
  47. mountPoint(mountPoint),
  48. files(listFiles(mountPoint, archive))
  49. {
  50. logGlobal->traceStream() << "Zip archive loaded, " << files.size() << " files found";
  51. }
  52. std::unordered_map<ResourceID, unz_file_pos> CZipLoader::listFiles(const std::string & mountPoint, const std::string & archive)
  53. {
  54. std::unordered_map<ResourceID, unz_file_pos> ret;
  55. unzFile file = unzOpen2_64(archive.c_str(), &zlibApi);
  56. if (unzGoToFirstFile(file) == UNZ_OK)
  57. {
  58. do
  59. {
  60. unz_file_info info;
  61. std::vector<char> filename;
  62. // Fill unz_file_info structure with current file info
  63. unzGetCurrentFileInfo (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  64. filename.resize(info.size_filename);
  65. // Get name of current file. Contrary to docs "info" parameter can't be null
  66. unzGetCurrentFileInfo (file, &info, filename.data(), filename.size(), nullptr, 0, nullptr, 0);
  67. std::string filenameString(filename.data(), filename.size());
  68. unzGetFilePos(file, &ret[ResourceID(mountPoint + filenameString)]);
  69. }
  70. while (unzGoToNextFile(file) == UNZ_OK);
  71. }
  72. unzClose(file);
  73. return ret;
  74. }
  75. std::unique_ptr<CInputStream> CZipLoader::load(const ResourceID & resourceName) const
  76. {
  77. return std::unique_ptr<CInputStream>(new CZipStream(ioApi, archiveName, files.at(resourceName)));
  78. }
  79. bool CZipLoader::existsResource(const ResourceID & resourceName) const
  80. {
  81. return files.count(resourceName) != 0;
  82. }
  83. std::string CZipLoader::getMountPoint() const
  84. {
  85. return mountPoint;
  86. }
  87. std::unordered_set<ResourceID> CZipLoader::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
  88. {
  89. std::unordered_set<ResourceID> foundID;
  90. for (auto & file : files)
  91. {
  92. if (filter(file.first))
  93. foundID.insert(file.first);
  94. }
  95. return foundID;
  96. }
  97. /// extracts currently selected file from zip into stream "where"
  98. static bool extractCurrent(unzFile file, std::ostream & where)
  99. {
  100. std::array<char, 8 * 1024> buffer;
  101. unzOpenCurrentFile(file);
  102. while (1)
  103. {
  104. int readSize = unzReadCurrentFile(file, buffer.data(), buffer.size());
  105. if (readSize < 0) // error
  106. break;
  107. if (readSize == 0) // end-of-file. Also performs CRC check
  108. return unzCloseCurrentFile(file) == UNZ_OK;
  109. if (readSize > 0) // successful read
  110. {
  111. where.write(buffer.data(), readSize);
  112. if (!where.good())
  113. break;
  114. }
  115. }
  116. // extraction failed. Close file and exit
  117. unzCloseCurrentFile(file);
  118. return false;
  119. }
  120. std::vector<std::string> ZipArchive::listFiles(std::string filename)
  121. {
  122. std::vector<std::string> ret;
  123. unzFile file = unzOpen(filename.c_str());
  124. if (unzGoToFirstFile(file) == UNZ_OK)
  125. {
  126. do
  127. {
  128. unz_file_info info;
  129. std::vector<char> filename;
  130. unzGetCurrentFileInfo (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  131. filename.resize(info.size_filename);
  132. // Get name of current file. Contrary to docs "info" parameter can't be null
  133. unzGetCurrentFileInfo (file, &info, filename.data(), filename.size(), nullptr, 0, nullptr, 0);
  134. ret.push_back(std::string(filename.data(), filename.size()));
  135. }
  136. while (unzGoToNextFile(file) == UNZ_OK);
  137. }
  138. unzClose(file);
  139. return ret;
  140. }
  141. bool ZipArchive::extract(std::string from, std::string where)
  142. {
  143. // Note: may not be fast enough for large archives (should NOT happen with mods)
  144. // because locating each file by name may be slow. Unlikely slower than decompression though
  145. return extract(from, where, listFiles(from));
  146. }
  147. bool ZipArchive::extract(std::string from, std::string where, std::vector<std::string> what)
  148. {
  149. unzFile archive = unzOpen(from.c_str());
  150. auto onExit = vstd::makeScopeGuard([&]()
  151. {
  152. unzClose(archive);
  153. });
  154. for (std::string & file : what)
  155. {
  156. if (unzLocateFile(archive, file.c_str(), 1) != UNZ_OK)
  157. return false;
  158. std::string fullName = where + '/' + file;
  159. std::string fullPath = fullName.substr(0, fullName.find_last_of("/"));
  160. boost::filesystem::create_directories(fullPath);
  161. // directory. No file to extract
  162. // TODO: better way to detect directory? Probably check return value of unzOpenCurrentFile?
  163. if (boost::algorithm::ends_with(file, "/"))
  164. continue;
  165. std::ofstream destFile(fullName, std::ofstream::binary);
  166. if (!destFile.good())
  167. return false;
  168. if (!extractCurrent(archive, destFile))
  169. return false;
  170. }
  171. return true;
  172. }