zip_packer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************************/
  2. /* zip_packer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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(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, NULL, &io);
  39. return zf != NULL ? 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, NULL) == ZIP_OK ? OK : FAILED;
  44. if (err == OK) {
  45. zf = NULL;
  46. }
  47. return err;
  48. }
  49. Error ZIPPacker::start_file(String p_path) {
  50. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  51. zip_fileinfo zipfi;
  52. OS::DateTime time = OS::get_singleton()->get_datetime();
  53. zipfi.tmz_date.tm_hour = time.hour;
  54. zipfi.tmz_date.tm_mday = time.day;
  55. zipfi.tmz_date.tm_min = time.minute;
  56. zipfi.tmz_date.tm_mon = time.month - 1;
  57. zipfi.tmz_date.tm_sec = time.second;
  58. zipfi.tmz_date.tm_year = time.year;
  59. zipfi.dosDate = 0;
  60. zipfi.external_fa = 0;
  61. zipfi.internal_fa = 0;
  62. int ret = zipOpenNewFileInZip(zf, p_path.utf8().get_data(), &zipfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
  63. return ret == ZIP_OK ? OK : FAILED;
  64. }
  65. Error ZIPPacker::write_file(Vector<uint8_t> p_data) {
  66. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  67. return zipWriteInFileInZip(zf, p_data.ptr(), p_data.size()) == ZIP_OK ? OK : FAILED;
  68. }
  69. Error ZIPPacker::close_file() {
  70. ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
  71. return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
  72. }
  73. void ZIPPacker::_bind_methods() {
  74. ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
  75. ClassDB::bind_method(D_METHOD("start_file", "path"), &ZIPPacker::start_file);
  76. ClassDB::bind_method(D_METHOD("write_file", "data"), &ZIPPacker::write_file);
  77. ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
  78. ClassDB::bind_method(D_METHOD("close"), &ZIPPacker::close);
  79. BIND_ENUM_CONSTANT(APPEND_CREATE);
  80. BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
  81. BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
  82. }
  83. ZIPPacker::ZIPPacker() {}
  84. ZIPPacker::~ZIPPacker() {
  85. if (fa.is_valid()) {
  86. close();
  87. }
  88. }