2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* zip_packer.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-09-30 13:13:33 +00:00
|
|
|
|
|
|
|
#include "zip_packer.h"
|
|
|
|
|
|
|
|
#include "core/io/zip_io.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
|
2023-09-09 22:22:53 +00:00
|
|
|
Error ZIPPacker::open(const String &p_path, ZipAppend p_append) {
|
2018-09-30 13:13:33 +00:00
|
|
|
if (fa.is_valid()) {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
zlib_filefunc_def io = zipio_create_io(&fa);
|
2022-12-06 17:40:46 +00:00
|
|
|
zf = zipOpen2(p_path.utf8().get_data(), p_append, nullptr, &io);
|
|
|
|
return zf != nullptr ? OK : FAILED;
|
2018-09-30 13:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error ZIPPacker::close() {
|
|
|
|
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker cannot be closed because it is not open.");
|
|
|
|
|
2022-12-06 17:40:46 +00:00
|
|
|
Error err = zipClose(zf, nullptr) == ZIP_OK ? OK : FAILED;
|
2022-11-13 11:50:40 +00:00
|
|
|
if (err == OK) {
|
2022-12-06 17:40:46 +00:00
|
|
|
DEV_ASSERT(fa == nullptr);
|
|
|
|
zf = nullptr;
|
2022-11-13 11:50:40 +00:00
|
|
|
}
|
2022-12-06 17:40:46 +00:00
|
|
|
|
2022-11-13 11:50:40 +00:00
|
|
|
return err;
|
2018-09-30 13:13:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-09 22:22:53 +00:00
|
|
|
Error ZIPPacker::start_file(const String &p_path) {
|
2018-09-30 13:13:33 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
|
|
|
|
|
|
|
zip_fileinfo zipfi;
|
|
|
|
|
|
|
|
OS::DateTime time = OS::get_singleton()->get_datetime();
|
|
|
|
|
2022-12-06 17:40:46 +00:00
|
|
|
zipfi.tmz_date.tm_sec = time.second;
|
|
|
|
zipfi.tmz_date.tm_min = time.minute;
|
2018-09-30 13:13:33 +00:00
|
|
|
zipfi.tmz_date.tm_hour = time.hour;
|
|
|
|
zipfi.tmz_date.tm_mday = time.day;
|
|
|
|
zipfi.tmz_date.tm_mon = time.month - 1;
|
|
|
|
zipfi.tmz_date.tm_year = time.year;
|
|
|
|
zipfi.dosDate = 0;
|
|
|
|
zipfi.internal_fa = 0;
|
2022-12-06 17:40:46 +00:00
|
|
|
zipfi.external_fa = 0;
|
2018-09-30 13:13:33 +00:00
|
|
|
|
2023-06-27 03:49:51 +00:00
|
|
|
int err = zipOpenNewFileInZip4(zf,
|
|
|
|
p_path.utf8().get_data(),
|
|
|
|
&zipfi,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
Z_DEFLATED,
|
|
|
|
Z_DEFAULT_COMPRESSION,
|
|
|
|
0,
|
|
|
|
-MAX_WBITS,
|
|
|
|
DEF_MEM_LEVEL,
|
|
|
|
Z_DEFAULT_STRATEGY,
|
|
|
|
nullptr,
|
|
|
|
0,
|
2024-01-09 05:46:16 +00:00
|
|
|
0, // "version made by", indicates the compatibility of the file attribute information (the `external_fa` field above).
|
2023-06-27 03:49:51 +00:00
|
|
|
1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
|
2022-12-06 17:40:46 +00:00
|
|
|
return err == ZIP_OK ? OK : FAILED;
|
2018-09-30 13:13:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-09 22:22:53 +00:00
|
|
|
Error ZIPPacker::write_file(const Vector<uint8_t> &p_data) {
|
2018-09-30 13:13:33 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
|
|
|
|
|
|
|
return zipWriteInFileInZip(zf, p_data.ptr(), p_data.size()) == ZIP_OK ? OK : FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error ZIPPacker::close_file() {
|
|
|
|
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
|
|
|
|
2022-11-13 11:50:40 +00:00
|
|
|
return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
|
2018-09-30 13:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZIPPacker::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
|
|
|
|
ClassDB::bind_method(D_METHOD("start_file", "path"), &ZIPPacker::start_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("write_file", "data"), &ZIPPacker::write_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("close"), &ZIPPacker::close);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(APPEND_CREATE);
|
|
|
|
BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
|
|
|
|
BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZIPPacker::ZIPPacker() {}
|
|
|
|
|
|
|
|
ZIPPacker::~ZIPPacker() {
|
|
|
|
if (fa.is_valid()) {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|