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
36Error 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
46Error 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 == nullptr);
52 zf = nullptr;
53 }
54
55 return err;
56}
57
58Error ZIPPacker::start_file(const String &p_path) {
59 ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
60
61 zip_fileinfo zipfi;
62
63 OS::DateTime time = OS::get_singleton()->get_datetime();
64
65 zipfi.tmz_date.tm_sec = time.second;
66 zipfi.tmz_date.tm_min = time.minute;
67 zipfi.tmz_date.tm_hour = time.hour;
68 zipfi.tmz_date.tm_mday = time.day;
69 zipfi.tmz_date.tm_mon = time.month - 1;
70 zipfi.tmz_date.tm_year = time.year;
71 zipfi.dosDate = 0;
72 zipfi.internal_fa = 0;
73 zipfi.external_fa = 0;
74
75 int err = zipOpenNewFileInZip(zf, p_path.utf8().get_data(), &zipfi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
76 return err == ZIP_OK ? OK : FAILED;
77}
78
79Error ZIPPacker::write_file(const Vector<uint8_t> &p_data) {
80 ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
81
82 return zipWriteInFileInZip(zf, p_data.ptr(), p_data.size()) == ZIP_OK ? OK : FAILED;
83}
84
85Error ZIPPacker::close_file() {
86 ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
87
88 return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
89}
90
91void 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
98 BIND_ENUM_CONSTANT(APPEND_CREATE);
99 BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
100 BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
101}
102
103ZIPPacker::ZIPPacker() {}
104
105ZIPPacker::~ZIPPacker() {
106 if (fa.is_valid()) {
107 close();
108 }
109}
110