CZipLoader.cpp 5.2 KB

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