CZipLoader.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * CZipLoader.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CZipLoader.h"
  12. #include "../ScopeGuard.h"
  13. #include "../texts/TextOperations.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. CZipStream::CZipStream(const std::shared_ptr<CIOApi> & api, const boost::filesystem::path & archive, unz64_file_pos filepos)
  16. {
  17. zlib_filefunc64_def zlibApi;
  18. zlibApi = api->getApiStructure();
  19. file = unzOpen2_64(archive.c_str(), &zlibApi);
  20. unzGoToFilePos64(file, &filepos);
  21. unzOpenCurrentFile(file);
  22. }
  23. CZipStream::~CZipStream()
  24. {
  25. unzCloseCurrentFile(file);
  26. unzClose(file);
  27. }
  28. si64 CZipStream::readMore(ui8 * data, si64 size)
  29. {
  30. return unzReadCurrentFile(file, data, static_cast<unsigned int>(size));
  31. }
  32. si64 CZipStream::getSize()
  33. {
  34. unz_file_info64 info;
  35. unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  36. return info.uncompressed_size;
  37. }
  38. ui32 CZipStream::calculateCRC32()
  39. {
  40. unz_file_info64 info;
  41. unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  42. return info.crc;
  43. }
  44. ///CZipLoader
  45. CZipLoader::CZipLoader(const std::string & mountPoint, const boost::filesystem::path & archive, std::shared_ptr<CIOApi> api):
  46. ioApi(std::move(api)),
  47. zlibApi(ioApi->getApiStructure()),
  48. archiveName(archive),
  49. mountPoint(mountPoint),
  50. files(listFiles(mountPoint, archive))
  51. {
  52. logGlobal->trace("Zip archive loaded, %d files found", files.size());
  53. }
  54. std::unordered_map<ResourcePath, unz64_file_pos> CZipLoader::listFiles(const std::string & mountPoint, const boost::filesystem::path & archive)
  55. {
  56. std::unordered_map<ResourcePath, unz64_file_pos> ret;
  57. unzFile file = unzOpen2_64(archive.c_str(), &zlibApi);
  58. if(file == nullptr)
  59. logGlobal->error("%s failed to open", archive.string());
  60. if (unzGoToFirstFile(file) == UNZ_OK)
  61. {
  62. do
  63. {
  64. unz_file_info64 info;
  65. std::vector<char> filename;
  66. // Fill unz_file_info structure with current file info
  67. unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  68. filename.resize(info.size_filename);
  69. // Get name of current file. Contrary to docs "info" parameter can't be null
  70. unzGetCurrentFileInfo64(file, &info, filename.data(), static_cast<uLong>(filename.size()), nullptr, 0, nullptr, 0);
  71. std::string filenameString(filename.data(), filename.size());
  72. unzGetFilePos64(file, &ret[ResourcePath(mountPoint + filenameString)]);
  73. }
  74. while (unzGoToNextFile(file) == UNZ_OK);
  75. }
  76. unzClose(file);
  77. return ret;
  78. }
  79. std::unique_ptr<CInputStream> CZipLoader::load(const ResourcePath & resourceName) const
  80. {
  81. return std::unique_ptr<CInputStream>(new CZipStream(ioApi, archiveName, files.at(resourceName)));
  82. }
  83. bool CZipLoader::existsResource(const ResourcePath & resourceName) const
  84. {
  85. return files.count(resourceName) != 0;
  86. }
  87. std::string CZipLoader::getMountPoint() const
  88. {
  89. return mountPoint;
  90. }
  91. std::unordered_set<ResourcePath> CZipLoader::getFilteredFiles(std::function<bool(const ResourcePath &)> filter) const
  92. {
  93. std::unordered_set<ResourcePath> foundID;
  94. for(const auto & file : files)
  95. {
  96. if (filter(file.first))
  97. foundID.insert(file.first);
  98. }
  99. return foundID;
  100. }
  101. std::string CZipLoader::getFullFileURI(const ResourcePath& resourceName) const
  102. {
  103. auto relativePath = TextOperations::Utf8TofilesystemPath(resourceName.getName());
  104. auto path = boost::filesystem::canonical(archiveName) / relativePath;
  105. return TextOperations::filesystemPathToUtf8(path);
  106. }
  107. std::time_t CZipLoader::getLastWriteTime(const ResourcePath& resourceName) const
  108. {
  109. auto path = boost::filesystem::canonical(archiveName);
  110. return boost::filesystem::last_write_time(path);
  111. }
  112. /// extracts currently selected file from zip into stream "where"
  113. static bool extractCurrent(unzFile file, std::ostream & where)
  114. {
  115. std::array<char, 8 * 1024> buffer{};
  116. unzOpenCurrentFile(file);
  117. while(true)
  118. {
  119. int readSize = unzReadCurrentFile(file, buffer.data(), static_cast<unsigned int>(buffer.size()));
  120. if (readSize < 0) // error
  121. break;
  122. if (readSize == 0) // end-of-file. Also performs CRC check
  123. return unzCloseCurrentFile(file) == UNZ_OK;
  124. if (readSize > 0) // successful read
  125. {
  126. where.write(buffer.data(), readSize);
  127. if (!where.good())
  128. break;
  129. }
  130. }
  131. // extraction failed. Close file and exit
  132. unzCloseCurrentFile(file);
  133. return false;
  134. }
  135. std::vector<std::string> ZipArchive::listFiles()
  136. {
  137. std::vector<std::string> ret;
  138. int result = unzGoToFirstFile(archive);
  139. if (result == UNZ_OK)
  140. {
  141. do
  142. {
  143. unz_file_info64 info;
  144. std::vector<char> zipFilename;
  145. unzGetCurrentFileInfo64 (archive, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  146. zipFilename.resize(info.size_filename);
  147. // Get name of current file. Contrary to docs "info" parameter can't be null
  148. unzGetCurrentFileInfo64(archive, &info, zipFilename.data(), static_cast<uLong>(zipFilename.size()), nullptr, 0, nullptr, 0);
  149. ret.emplace_back(zipFilename.data(), zipFilename.size());
  150. result = unzGoToNextFile(archive);
  151. }
  152. while (result == UNZ_OK);
  153. }
  154. return ret;
  155. }
  156. ZipArchive::ZipArchive(const boost::filesystem::path & from)
  157. {
  158. CDefaultIOApi zipAPI;
  159. #if MINIZIP_NEEDS_32BIT_FUNCS
  160. auto zipStructure = zipAPI.getApiStructure32();
  161. archive = unzOpen2(from.c_str(), &zipStructure);
  162. #else
  163. auto zipStructure = zipAPI.getApiStructure();
  164. archive = unzOpen2_64(from.c_str(), &zipStructure);
  165. #endif
  166. if (archive == nullptr)
  167. throw std::runtime_error("Failed to open file '" + from.string());
  168. }
  169. ZipArchive::~ZipArchive()
  170. {
  171. unzClose(archive);
  172. }
  173. bool ZipArchive::extract(const boost::filesystem::path & where, const std::vector<std::string> & what)
  174. {
  175. for (const std::string & file : what)
  176. if (!extract(where, file))
  177. return false;
  178. return true;
  179. }
  180. bool ZipArchive::extract(const boost::filesystem::path & where, const std::string & file)
  181. {
  182. if (unzLocateFile(archive, file.c_str(), 1) != UNZ_OK)
  183. return false;
  184. const boost::filesystem::path fullName = where / file;
  185. const boost::filesystem::path fullPath = fullName.parent_path();
  186. boost::filesystem::create_directories(fullPath);
  187. // directory. No file to extract
  188. // TODO: better way to detect directory? Probably check return value of unzOpenCurrentFile?
  189. if (boost::algorithm::ends_with(file, "/"))
  190. return true;
  191. std::fstream destFile(fullName.c_str(), std::ios::out | std::ios::binary);
  192. if (!destFile.good())
  193. {
  194. #ifdef VCMI_WINDOWS
  195. if (fullName.size() < 260)
  196. logGlobal->error("Failed to open file '%s'", fullName.c_str());
  197. else
  198. logGlobal->error("Failed to open file with long path '%s' (%d characters)", fullName.c_str(), fullName.size());
  199. #else
  200. logGlobal->error("Failed to open file '%s'", fullName.c_str());
  201. #endif
  202. return false;
  203. }
  204. if (!extractCurrent(archive, destFile))
  205. return false;
  206. return true;
  207. }
  208. VCMI_LIB_NAMESPACE_END