zip_packer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**************************************************************************/
  2. /* zip_packer.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_packer.h"
  31. #include "core/io/zip_io.h"
  32. #include "core/os/os.h"
  33. Error ZIPPacker::open(const String &p_path, ZipAppend p_append) {
  34. if (fa.is_valid()) {
  35. close();
  36. }
  37. zlib_filefunc_def io = zipio_create_io(&fa);
  38. zf = zipOpen2(p_path.utf8().get_data(), p_append, nullptr, &io);
  39. return zf != nullptr ? OK : FAILED;
  40. }
  41. Error ZIPPacker::close() {
  42. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker cannot be closed because it is not open.");
  43. Error err = zipClose(zf, nullptr) == ZIP_OK ? OK : FAILED;
  44. if (err == OK) {
  45. DEV_ASSERT(fa == nullptr);
  46. zf = nullptr;
  47. }
  48. return err;
  49. }
  50. Error ZIPPacker::start_file(const String &p_path) {
  51. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  52. zip_fileinfo zipfi;
  53. OS::DateTime time = OS::get_singleton()->get_datetime();
  54. zipfi.tmz_date.tm_sec = time.second;
  55. zipfi.tmz_date.tm_min = time.minute;
  56. zipfi.tmz_date.tm_hour = time.hour;
  57. zipfi.tmz_date.tm_mday = time.day;
  58. zipfi.tmz_date.tm_mon = time.month - 1;
  59. zipfi.tmz_date.tm_year = time.year;
  60. zipfi.dosDate = 0;
  61. zipfi.internal_fa = 0;
  62. zipfi.external_fa = 0;
  63. int err = zipOpenNewFileInZip4(zf,
  64. p_path.utf8().get_data(),
  65. &zipfi,
  66. nullptr,
  67. 0,
  68. nullptr,
  69. 0,
  70. nullptr,
  71. Z_DEFLATED,
  72. Z_DEFAULT_COMPRESSION,
  73. 0,
  74. -MAX_WBITS,
  75. DEF_MEM_LEVEL,
  76. Z_DEFAULT_STRATEGY,
  77. nullptr,
  78. 0,
  79. 0, // "version made by", indicates the compatibility of the file attribute information (the `external_fa` field above).
  80. 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
  81. return err == ZIP_OK ? OK : FAILED;
  82. }
  83. Error ZIPPacker::write_file(const Vector<uint8_t> &p_data) {
  84. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  85. return zipWriteInFileInZip(zf, p_data.ptr(), p_data.size()) == ZIP_OK ? OK : FAILED;
  86. }
  87. Error ZIPPacker::close_file() {
  88. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  89. return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
  90. }
  91. void ZIPPacker::_bind_methods() {
  92. ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
  93. ClassDB::bind_method(D_METHOD("start_file", "path"), &ZIPPacker::start_file);
  94. ClassDB::bind_method(D_METHOD("write_file", "data"), &ZIPPacker::write_file);
  95. ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
  96. ClassDB::bind_method(D_METHOD("close"), &ZIPPacker::close);
  97. BIND_ENUM_CONSTANT(APPEND_CREATE);
  98. BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
  99. BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
  100. }
  101. ZIPPacker::ZIPPacker() {}
  102. ZIPPacker::~ZIPPacker() {
  103. if (fa.is_valid()) {
  104. close();
  105. }
  106. }