2
0

CZipLoader.cpp 5.4 KB

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