zip_reader.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**************************************************************************/
  2. /* zip_reader.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "zip_reader.h"
  31. #include "core/error/error_macros.h"
  32. #include "core/io/zip_io.h"
  33. Error ZIPReader::open(const String &p_path) {
  34. if (fa.is_valid()) {
  35. close();
  36. }
  37. zlib_filefunc_def io = zipio_create_io(&fa);
  38. uzf = unzOpen2(p_path.utf8().get_data(), &io);
  39. return uzf != nullptr ? OK : FAILED;
  40. }
  41. Error ZIPReader::close() {
  42. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPReader cannot be closed because it is not open.");
  43. Error err = unzClose(uzf) == UNZ_OK ? OK : FAILED;
  44. if (err == OK) {
  45. DEV_ASSERT(fa.is_null());
  46. uzf = nullptr;
  47. }
  48. return err;
  49. }
  50. PackedStringArray ZIPReader::get_files() {
  51. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), "ZIPReader must be opened before use.");
  52. unz_global_info gi;
  53. int err = unzGetGlobalInfo(uzf, &gi);
  54. ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
  55. if (gi.number_entry == 0) {
  56. return PackedStringArray();
  57. }
  58. err = unzGoToFirstFile(uzf);
  59. ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
  60. List<String> s;
  61. do {
  62. unz_file_info64 file_info;
  63. String filepath;
  64. err = godot_unzip_get_current_file_info(uzf, file_info, filepath);
  65. if (err == UNZ_OK) {
  66. s.push_back(filepath);
  67. }
  68. } while (unzGoToNextFile(uzf) == UNZ_OK);
  69. PackedStringArray arr;
  70. arr.resize(s.size());
  71. int idx = 0;
  72. for (const List<String>::Element *E = s.front(); E; E = E->next()) {
  73. arr.set(idx++, E->get());
  74. }
  75. return arr;
  76. }
  77. PackedByteArray ZIPReader::read_file(const String &p_path, bool p_case_sensitive) {
  78. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use.");
  79. int err = UNZ_OK;
  80. // Locate and open the file.
  81. err = godot_unzip_locate_file(uzf, p_path, p_case_sensitive);
  82. ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "File does not exist in zip archive: " + p_path);
  83. err = unzOpenCurrentFile(uzf);
  84. ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Could not open file within zip archive.");
  85. // Read the file info.
  86. unz_file_info info;
  87. err = unzGetCurrentFileInfo(uzf, &info, nullptr, 0, nullptr, 0, nullptr, 0);
  88. ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Unable to read file information from zip archive.");
  89. ERR_FAIL_COND_V_MSG(info.uncompressed_size > INT_MAX, PackedByteArray(), "File contents too large to read from zip archive (>2 GB).");
  90. // Read the file data.
  91. PackedByteArray data;
  92. data.resize(info.uncompressed_size);
  93. uint8_t *buffer = data.ptrw();
  94. int to_read = data.size();
  95. while (to_read > 0) {
  96. int bytes_read = unzReadCurrentFile(uzf, buffer, to_read);
  97. ERR_FAIL_COND_V_MSG(bytes_read < 0, PackedByteArray(), "IO/zlib error reading file from zip archive.");
  98. ERR_FAIL_COND_V_MSG(bytes_read == UNZ_EOF && to_read != 0, PackedByteArray(), "Incomplete file read from zip archive.");
  99. DEV_ASSERT(bytes_read <= to_read);
  100. buffer += bytes_read;
  101. to_read -= bytes_read;
  102. }
  103. // Verify the data and return.
  104. err = unzCloseCurrentFile(uzf);
  105. ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "CRC error reading file from zip archive.");
  106. return data;
  107. }
  108. bool ZIPReader::file_exists(const String &p_path, bool p_case_sensitive) {
  109. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "ZIPReader must be opened before use.");
  110. int cs = p_case_sensitive ? 1 : 2;
  111. if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
  112. return false;
  113. }
  114. if (unzOpenCurrentFile(uzf) != UNZ_OK) {
  115. return false;
  116. }
  117. unzCloseCurrentFile(uzf);
  118. return true;
  119. }
  120. ZIPReader::ZIPReader() {}
  121. ZIPReader::~ZIPReader() {
  122. if (fa.is_valid()) {
  123. close();
  124. }
  125. }
  126. void ZIPReader::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("open", "path"), &ZIPReader::open);
  128. ClassDB::bind_method(D_METHOD("close"), &ZIPReader::close);
  129. ClassDB::bind_method(D_METHOD("get_files"), &ZIPReader::get_files);
  130. ClassDB::bind_method(D_METHOD("read_file", "path", "case_sensitive"), &ZIPReader::read_file, DEFVAL(Variant(true)));
  131. ClassDB::bind_method(D_METHOD("file_exists", "path", "case_sensitive"), &ZIPReader::file_exists, DEFVAL(Variant(true)));
  132. }