zip_packer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.is_null());
  46. zf = nullptr;
  47. }
  48. return err;
  49. }
  50. void ZIPPacker::set_compression_level(int p_compression_level) {
  51. ERR_FAIL_COND_MSG(p_compression_level < Z_DEFAULT_COMPRESSION || p_compression_level > Z_BEST_COMPRESSION, "Invalid compression level.");
  52. compression_level = p_compression_level;
  53. }
  54. int ZIPPacker::get_compression_level() const {
  55. return compression_level;
  56. }
  57. Error ZIPPacker::start_file(const String &p_path) {
  58. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  59. zip_fileinfo zipfi;
  60. OS::DateTime time = OS::get_singleton()->get_datetime();
  61. zipfi.tmz_date.tm_sec = time.second;
  62. zipfi.tmz_date.tm_min = time.minute;
  63. zipfi.tmz_date.tm_hour = time.hour;
  64. zipfi.tmz_date.tm_mday = time.day;
  65. zipfi.tmz_date.tm_mon = time.month - 1;
  66. zipfi.tmz_date.tm_year = time.year;
  67. zipfi.dosDate = 0;
  68. zipfi.internal_fa = 0;
  69. zipfi.external_fa = 0;
  70. int err = zipOpenNewFileInZip4(zf,
  71. p_path.utf8().get_data(),
  72. &zipfi,
  73. nullptr,
  74. 0,
  75. nullptr,
  76. 0,
  77. nullptr,
  78. Z_DEFLATED,
  79. compression_level,
  80. 0,
  81. -MAX_WBITS,
  82. DEF_MEM_LEVEL,
  83. Z_DEFAULT_STRATEGY,
  84. nullptr,
  85. 0,
  86. 0, // "version made by", indicates the compatibility of the file attribute information (the `external_fa` field above).
  87. 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
  88. return err == ZIP_OK ? OK : FAILED;
  89. }
  90. Error ZIPPacker::write_file(const Vector<uint8_t> &p_data) {
  91. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  92. return zipWriteInFileInZip(zf, p_data.ptr(), p_data.size()) == ZIP_OK ? OK : FAILED;
  93. }
  94. Error ZIPPacker::close_file() {
  95. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  96. return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
  97. }
  98. void ZIPPacker::_bind_methods() {
  99. ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
  100. ClassDB::bind_method(D_METHOD("set_compression_level", "compression_level"), &ZIPPacker::set_compression_level);
  101. ClassDB::bind_method(D_METHOD("get_compression_level"), &ZIPPacker::get_compression_level);
  102. ADD_PROPERTY(PropertyInfo(Variant::INT, "compression_level"), "set_compression_level", "get_compression_level");
  103. ClassDB::bind_method(D_METHOD("start_file", "path"), &ZIPPacker::start_file);
  104. ClassDB::bind_method(D_METHOD("write_file", "data"), &ZIPPacker::write_file);
  105. ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
  106. ClassDB::bind_method(D_METHOD("close"), &ZIPPacker::close);
  107. BIND_ENUM_CONSTANT(APPEND_CREATE);
  108. BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
  109. BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
  110. BIND_ENUM_CONSTANT(COMPRESSION_DEFAULT);
  111. BIND_ENUM_CONSTANT(COMPRESSION_NONE);
  112. BIND_ENUM_CONSTANT(COMPRESSION_FAST);
  113. BIND_ENUM_CONSTANT(COMPRESSION_BEST);
  114. }
  115. ZIPPacker::ZIPPacker() {
  116. }
  117. ZIPPacker::~ZIPPacker() {
  118. if (fa.is_valid()) {
  119. close();
  120. }
  121. }