Debian Codegraph @

modules/zip/zip_packer.cpp

raw symbols SCIP
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
31 #include "zip_packer.h"
32
33 #include "core/io/zip_io.h"
34 #include "core/os/os.h"
35
36 Error ZIPPacker::open(const String &p_path, ZipAppend p_append) {
37 if (fa.is_valid()) {
38 close();
39 }
40
41 zlib_filefunc_def io = zipio_create_io(&fa);
42 zf = zipOpen2(p_path.utf8().get_data(), p_append, nullptr, &io);
43 return zf != nullptr ? OK : FAILED;
44 }
45
46 Error ZIPPacker::close() {
47 ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker cannot be closed because it is not open.");
48
49 Error err = zipClose(zf, nullptr) == ZIP_OK ? OK : FAILED;
50 if (err == OK) {
51 DEV_ASSERT(fa.is_null());
52 zf = nullptr;
53 }
54
55 return err;
56 }
57
58 void ZIPPacker::set_compression_level(int p_compression_level) {
59 ERR_FAIL_COND_MSG(p_compression_level < Z_DEFAULT_COMPRESSION || p_compression_level > Z_BEST_COMPRESSION, "Invalid compression level.");
60 compression_level = p_compression_level;
61 }
62
63 int ZIPPacker::get_compression_level() const {
64 return compression_level;
65 }
66
67 Error ZIPPacker::start_file(const String &p_path) {
68 ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
69
70 zip_fileinfo zipfi;
71
72 OS::DateTime time = OS::get_singleton()->get_datetime();
73
74 zipfi.tmz_date.tm_sec = time.second;
75 zipfi.tmz_date.tm_min = time.minute;
76 zipfi.tmz_date.tm_hour = time.hour;
77 zipfi.tmz_date.tm_mday = time.day;
78 zipfi.tmz_date.tm_mon = time.month - 1;
79 zipfi.tmz_date.tm_year = time.year;
80 zipfi.dosDate = 0;
81 zipfi.internal_fa = 0;
82 zipfi.external_fa = 0;
83
84 int err = zipOpenNewFileInZip4(zf,
85 p_path.utf8().get_data(),
86 &zipfi,
87 nullptr,
88 0,
89 nullptr,
90 0,
91 nullptr,
92 Z_DEFLATED,
93 compression_level,
94 0,
95 -MAX_WBITS,
96 DEF_MEM_LEVEL,
97 Z_DEFAULT_STRATEGY,
98 nullptr,
99 0,
100 0, // "version made by", indicates the compatibility of the file attribute information (the `external_fa` field above).
101 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
102 return err == ZIP_OK ? OK : FAILED;
103 }
104
105 Error ZIPPacker::write_file(const Vector<uint8_t> &p_data) {
106 ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
107
108 return zipWriteInFileInZip(zf, p_data.ptr(), p_data.size()) == ZIP_OK ? OK : FAILED;
109 }
110
111 Error ZIPPacker::close_file() {
112 ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
113
114 return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
115 }
116
117 void ZIPPacker::_bind_methods() {
118 ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
119 ClassDB::bind_method(D_METHOD("set_compression_level", "compression_level"), &ZIPPacker::set_compression_level);
120 ClassDB::bind_method(D_METHOD("get_compression_level"), &ZIPPacker::get_compression_level);
121 ADD_PROPERTY(PropertyInfo(Variant::INT, "compression_level"), "set_compression_level", "get_compression_level");
122 ClassDB::bind_method(D_METHOD("start_file", "path"), &ZIPPacker::start_file);
123 ClassDB::bind_method(D_METHOD("write_file", "data"), &ZIPPacker::write_file);
124 ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
125 ClassDB::bind_method(D_METHOD("close"), &ZIPPacker::close);
126
127 BIND_ENUM_CONSTANT(APPEND_CREATE);
128 BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
129 BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
130
131 BIND_ENUM_CONSTANT(COMPRESSION_DEFAULT);
132 BIND_ENUM_CONSTANT(COMPRESSION_NONE);
133 BIND_ENUM_CONSTANT(COMPRESSION_FAST);
134 BIND_ENUM_CONSTANT(COMPRESSION_BEST);
135 }
136
137 ZIPPacker::ZIPPacker() {
138 }
139
140 ZIPPacker::~ZIPPacker() {
141 if (fa.is_valid()) {
142 close();
143 }
144 }