zip_reader.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(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 != NULL ? 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. return unzClose(uzf) == UNZ_OK ? OK : FAILED;
  44. }
  45. PackedStringArray ZIPReader::get_files() {
  46. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), "ZIPReader must be opened before use.");
  47. List<String> s;
  48. if (unzGoToFirstFile(uzf) != UNZ_OK) {
  49. return PackedStringArray();
  50. }
  51. do {
  52. unz_file_info64 file_info;
  53. char filename[256]; // Note filename is a path !
  54. int err = unzGetCurrentFileInfo64(uzf, &file_info, filename, sizeof(filename), NULL, 0, NULL, 0);
  55. if (err == UNZ_OK) {
  56. s.push_back(filename);
  57. } else {
  58. // Assume filename buffer was too small
  59. char *long_filename_buff = (char *)memalloc(file_info.size_filename);
  60. int err2 = unzGetCurrentFileInfo64(uzf, NULL, long_filename_buff, sizeof(long_filename_buff), NULL, 0, NULL, 0);
  61. if (err2 == UNZ_OK) {
  62. s.push_back(long_filename_buff);
  63. memfree(long_filename_buff);
  64. }
  65. }
  66. } while (unzGoToNextFile(uzf) == UNZ_OK);
  67. PackedStringArray arr;
  68. arr.resize(s.size());
  69. int idx = 0;
  70. for (const List<String>::Element *E = s.front(); E; E = E->next()) {
  71. arr.set(idx++, E->get());
  72. }
  73. return arr;
  74. }
  75. PackedByteArray ZIPReader::read_file(String p_path, bool p_case_sensitive) {
  76. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use.");
  77. int cs = p_case_sensitive ? 1 : 2;
  78. if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
  79. ERR_FAIL_V_MSG(PackedByteArray(), "File does not exist in zip archive: " + p_path);
  80. }
  81. if (unzOpenCurrentFile(uzf) != UNZ_OK) {
  82. ERR_FAIL_V_MSG(PackedByteArray(), "Could not open file within zip archive.");
  83. }
  84. unz_file_info info;
  85. unzGetCurrentFileInfo(uzf, &info, NULL, 0, NULL, 0, NULL, 0);
  86. PackedByteArray data;
  87. data.resize(info.uncompressed_size);
  88. uint8_t *w = data.ptrw();
  89. unzReadCurrentFile(uzf, &w[0], info.uncompressed_size);
  90. unzCloseCurrentFile(uzf);
  91. return data;
  92. }
  93. ZIPReader::ZIPReader() {}
  94. ZIPReader::~ZIPReader() {
  95. if (fa.is_valid()) {
  96. close();
  97. }
  98. }
  99. void ZIPReader::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("open", "path"), &ZIPReader::open);
  101. ClassDB::bind_method(D_METHOD("close"), &ZIPReader::close);
  102. ClassDB::bind_method(D_METHOD("get_files"), &ZIPReader::get_files);
  103. ClassDB::bind_method(D_METHOD("read_file", "path", "case_sensitive"), &ZIPReader::read_file, DEFVAL(Variant(true)));
  104. }