initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled

This commit is contained in:
2025-09-16 20:46:46 -04:00
commit 9d30169a8d
13378 changed files with 7050105 additions and 0 deletions

14
modules/zip/SCsub Normal file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
Import("env_modules")
env_zip = env_modules.Clone()
# Module files
env_zip.add_source_files(env.modules_sources, "*.cpp")
if env["tests"]:
env_zip.Append(CPPDEFINES=["TESTS_ENABLED"])
env_zip.add_source_files(env.modules_sources, "./tests/*.cpp")

17
modules/zip/config.py Normal file
View File

@@ -0,0 +1,17 @@
def can_build(env, platform):
return env["minizip"]
def configure(env):
pass
def get_doc_classes():
return [
"ZIPReader",
"ZIPPacker",
]
def get_doc_path():
return "doc_classes"

View File

@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="ZIPPacker" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Allows the creation of ZIP files.
</brief_description>
<description>
This class implements a writer that allows storing the multiple blobs in a ZIP archive. See also [ZIPReader] and [PCKPacker].
[codeblock]
# Create a ZIP archive with a single file at its root.
func write_zip_file():
var writer = ZIPPacker.new()
var err = writer.open("user://archive.zip")
if err != OK:
return err
writer.start_file("hello.txt")
writer.write_file("Hello World".to_utf8_buffer())
writer.close_file()
writer.close()
return OK
[/codeblock]
</description>
<tutorials>
</tutorials>
<methods>
<method name="close">
<return type="int" enum="Error" />
<description>
Closes the underlying resources used by this instance.
</description>
</method>
<method name="close_file">
<return type="int" enum="Error" />
<description>
Stops writing to a file within the archive.
It will fail if there is no open file.
</description>
</method>
<method name="open">
<return type="int" enum="Error" />
<param index="0" name="path" type="String" />
<param index="1" name="append" type="int" enum="ZIPPacker.ZipAppend" default="0" />
<description>
Opens a zip file for writing at the given path using the specified write mode.
This must be called before everything else.
</description>
</method>
<method name="start_file">
<return type="int" enum="Error" />
<param index="0" name="path" type="String" />
<description>
Starts writing to a file within the archive. Only one file can be written at the same time.
Must be called after [method open].
</description>
</method>
<method name="write_file">
<return type="int" enum="Error" />
<param index="0" name="data" type="PackedByteArray" />
<description>
Write the given [param data] to the file.
Needs to be called after [method start_file].
</description>
</method>
</methods>
<members>
<member name="compression_level" type="int" setter="set_compression_level" getter="get_compression_level" default="-1">
The compression level used when [method start_file] is called. Use [enum ZIPPacker.CompressionLevel] as a reference.
</member>
</members>
<constants>
<constant name="APPEND_CREATE" value="0" enum="ZipAppend">
Create a new zip archive at the given path.
</constant>
<constant name="APPEND_CREATEAFTER" value="1" enum="ZipAppend">
Append a new zip archive to the end of the already existing file at the given path.
</constant>
<constant name="APPEND_ADDINZIP" value="2" enum="ZipAppend">
Add new files to the existing zip archive at the given path.
</constant>
<constant name="COMPRESSION_DEFAULT" value="-1" enum="CompressionLevel">
Start a file with the default Deflate compression level ([code]6[/code]). This is a good compromise between speed and file size.
</constant>
<constant name="COMPRESSION_NONE" value="0" enum="CompressionLevel">
Start a file with no compression. This is also known as the "Store" compression mode and is the fastest method of packing files inside a ZIP archive. Consider using this mode for files that are already compressed (such as JPEG, PNG, MP3, or Ogg Vorbis files).
</constant>
<constant name="COMPRESSION_FAST" value="1" enum="CompressionLevel">
Start a file with the fastest Deflate compression level ([code]1[/code]). This is fast to compress, but results in larger file sizes than [constant COMPRESSION_DEFAULT]. Decompression speed is generally unaffected by the chosen compression level.
</constant>
<constant name="COMPRESSION_BEST" value="9" enum="CompressionLevel">
Start a file with the best Deflate compression level ([code]9[/code]). This is slow to compress, but results in smaller file sizes than [constant COMPRESSION_DEFAULT]. Decompression speed is generally unaffected by the chosen compression level.
</constant>
</constants>
</class>

View File

@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="ZIPReader" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Allows reading the content of a ZIP file.
</brief_description>
<description>
This class implements a reader that can extract the content of individual files inside a ZIP archive. See also [ZIPPacker].
[codeblock]
# Read a single file from a ZIP archive.
func read_zip_file():
var reader = ZIPReader.new()
var err = reader.open("user://archive.zip")
if err != OK:
return PackedByteArray()
var res = reader.read_file("hello.txt")
reader.close()
return res
# Extract all files from a ZIP archive, preserving the directories within.
# This acts like the "Extract all" functionality from most archive managers.
func extract_all_from_zip():
var reader = ZIPReader.new()
reader.open("res://archive.zip")
# Destination directory for the extracted files (this folder must exist before extraction).
# Not all ZIP archives put everything in a single root folder,
# which means several files/folders may be created in `root_dir` after extraction.
var root_dir = DirAccess.open("user://")
var files = reader.get_files()
for file_path in files:
# If the current entry is a directory.
if file_path.ends_with("/"):
root_dir.make_dir_recursive(file_path)
continue
# Write file contents, creating folders automatically when needed.
# Not all ZIP archives are strictly ordered, so we need to do this in case
# the file entry comes before the folder entry.
root_dir.make_dir_recursive(root_dir.get_current_dir().path_join(file_path).get_base_dir())
var file = FileAccess.open(root_dir.get_current_dir().path_join(file_path), FileAccess.WRITE)
var buffer = reader.read_file(file_path)
file.store_buffer(buffer)
[/codeblock]
</description>
<tutorials>
</tutorials>
<methods>
<method name="close">
<return type="int" enum="Error" />
<description>
Closes the underlying resources used by this instance.
</description>
</method>
<method name="file_exists">
<return type="bool" />
<param index="0" name="path" type="String" />
<param index="1" name="case_sensitive" type="bool" default="true" />
<description>
Returns [code]true[/code] if the file exists in the loaded zip archive.
Must be called after [method open].
</description>
</method>
<method name="get_compression_level">
<return type="int" />
<param index="0" name="path" type="String" />
<param index="1" name="case_sensitive" type="bool" default="true" />
<description>
Returns the compression level of the file in the loaded zip archive. Returns [code]-1[/code] if the file doesn't exist or any other error occurs. Must be called after [method open].
</description>
</method>
<method name="get_files">
<return type="PackedStringArray" />
<description>
Returns the list of names of all files in the loaded archive.
Must be called after [method open].
</description>
</method>
<method name="open">
<return type="int" enum="Error" />
<param index="0" name="path" type="String" />
<description>
Opens the zip archive at the given [param path] and reads its file index.
</description>
</method>
<method name="read_file">
<return type="PackedByteArray" />
<param index="0" name="path" type="String" />
<param index="1" name="case_sensitive" type="bool" default="true" />
<description>
Loads the whole content of a file in the loaded zip archive into memory and returns it.
Must be called after [method open].
</description>
</method>
</methods>
</class>

View File

@@ -0,0 +1,51 @@
/**************************************************************************/
/* register_types.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. */
/**************************************************************************/
#include "register_types.h"
#include "zip_packer.h"
#include "zip_reader.h"
#include "core/object/class_db.h"
void initialize_zip_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
GDREGISTER_CLASS(ZIPPacker);
GDREGISTER_CLASS(ZIPReader);
}
void uninitialize_zip_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}

View File

@@ -0,0 +1,36 @@
/**************************************************************************/
/* register_types.h */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#pragma once
#include "modules/register_module_types.h"
void initialize_zip_module(ModuleInitializationLevel p_level);
void uninitialize_zip_module(ModuleInitializationLevel p_level);

Binary file not shown.

View File

@@ -0,0 +1,41 @@
/**************************************************************************/
/* test_zip.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. */
/**************************************************************************/
#include "test_zip.h"
namespace TestZip {
void check_file_size(const String &p_path, int p_expected_size) {
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
CHECK(f.is_valid());
CHECK(f->get_length() == p_expected_size);
}
} // namespace TestZip

View File

@@ -0,0 +1,105 @@
/**************************************************************************/
/* test_zip.h */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#pragma once
#include "tests/test_macros.h"
#include "tests/test_utils.h"
#include "../zip_packer.h"
#include "../zip_reader.h"
namespace TestZip {
void check_file_size(const String &p_path, int p_expected_size);
TEST_CASE("[ZIPPacker] default compression") {
const String path = TestUtils::get_temp_path("compressed.zip");
Ref<ZIPPacker> packer;
packer.instantiate();
Error open_result = packer->open(path, ZIPPacker::APPEND_CREATE);
CHECK(open_result == OK);
Error start_file_result = packer->start_file("demo.txt");
CHECK(start_file_result == OK);
String text = "hello world!";
Error write_file_result = packer->write_file(text.to_utf8_buffer());
CHECK(write_file_result == OK);
Error close_file_result = packer->close_file();
CHECK(close_file_result == OK);
Error close_result = packer->close();
CHECK(close_result == OK);
check_file_size(path, 128);
}
TEST_CASE("[ZIPPacker] no compression") {
const String path = TestUtils::get_temp_path("uncompressed.zip");
Ref<ZIPPacker> packer;
packer.instantiate();
Error open_result = packer->open(path, ZIPPacker::APPEND_CREATE);
CHECK(open_result == OK);
packer->set_compression_level(ZIPPacker::COMPRESSION_NONE);
Error start_file_result = packer->start_file("demo.txt");
CHECK(start_file_result == OK);
String text = "hello world!";
Error write_file_result = packer->write_file(text.to_utf8_buffer());
CHECK(write_file_result == OK);
Error close_file_result = packer->close_file();
CHECK(close_file_result == OK);
Error close_result = packer->close();
CHECK(close_result == OK);
check_file_size(path, 131);
}
TEST_CASE("[ZIPReader] read files") {
String test_data = String("modules/zip/tests/data/").path_join("test.zip");
Ref<ZIPReader> reader;
reader.instantiate();
Error open_result = reader->open(test_data);
CHECK(open_result == OK);
const String hello_path = "hello.txt";
const String world_path = "world.txt";
PackedStringArray expected_files;
expected_files.push_back(hello_path);
expected_files.push_back(world_path);
CHECK(reader->get_files() == expected_files);
const String expected_hello_text = "hello world!";
const String expected_world_text = "game over!";
PackedByteArray hello_bytes = reader->read_file(hello_path, false);
PackedByteArray world_bytes = reader->read_file(world_path, true);
CHECK(hello_bytes == expected_hello_text.to_utf8_buffer());
CHECK(world_bytes == expected_world_text.to_utf8_buffer());
CHECK(reader->get_compression_level(hello_path, true) == 6);
CHECK(reader->get_compression_level(world_path, false) == 9);
}
} // namespace TestZip

144
modules/zip/zip_packer.cpp Normal file
View File

@@ -0,0 +1,144 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#include "zip_packer.h"
#include "core/io/zip_io.h"
#include "core/os/os.h"
Error ZIPPacker::open(const String &p_path, ZipAppend p_append) {
if (fa.is_valid()) {
close();
}
zlib_filefunc_def io = zipio_create_io(&fa);
zf = zipOpen2(p_path.utf8().get_data(), p_append, nullptr, &io);
return zf != nullptr ? OK : FAILED;
}
Error ZIPPacker::close() {
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker cannot be closed because it is not open.");
Error err = zipClose(zf, nullptr) == ZIP_OK ? OK : FAILED;
if (err == OK) {
DEV_ASSERT(fa.is_null());
zf = nullptr;
}
return err;
}
void ZIPPacker::set_compression_level(int p_compression_level) {
ERR_FAIL_COND_MSG(p_compression_level < Z_DEFAULT_COMPRESSION || p_compression_level > Z_BEST_COMPRESSION, "Invalid compression level.");
compression_level = p_compression_level;
}
int ZIPPacker::get_compression_level() const {
return compression_level;
}
Error ZIPPacker::start_file(const String &p_path) {
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();
zipfi.tmz_date.tm_sec = time.second;
zipfi.tmz_date.tm_min = time.minute;
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;
zipfi.external_fa = 0;
int err = zipOpenNewFileInZip4(zf,
p_path.utf8().get_data(),
&zipfi,
nullptr,
0,
nullptr,
0,
nullptr,
Z_DEFLATED,
compression_level,
0,
-MAX_WBITS,
DEF_MEM_LEVEL,
Z_DEFAULT_STRATEGY,
nullptr,
0,
0, // "version made by", indicates the compatibility of the file attribute information (the `external_fa` field above).
1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
return err == ZIP_OK ? OK : FAILED;
}
Error ZIPPacker::write_file(const Vector<uint8_t> &p_data) {
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.");
return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
}
void ZIPPacker::_bind_methods() {
ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
ClassDB::bind_method(D_METHOD("set_compression_level", "compression_level"), &ZIPPacker::set_compression_level);
ClassDB::bind_method(D_METHOD("get_compression_level"), &ZIPPacker::get_compression_level);
ADD_PROPERTY(PropertyInfo(Variant::INT, "compression_level"), "set_compression_level", "get_compression_level");
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);
BIND_ENUM_CONSTANT(COMPRESSION_DEFAULT);
BIND_ENUM_CONSTANT(COMPRESSION_NONE);
BIND_ENUM_CONSTANT(COMPRESSION_FAST);
BIND_ENUM_CONSTANT(COMPRESSION_BEST);
}
ZIPPacker::ZIPPacker() {
}
ZIPPacker::~ZIPPacker() {
if (fa.is_valid()) {
close();
}
}

77
modules/zip/zip_packer.h Normal file
View File

@@ -0,0 +1,77 @@
/**************************************************************************/
/* zip_packer.h */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#pragma once
#include "core/io/file_access.h"
#include "core/object/ref_counted.h"
#include "thirdparty/minizip/zip.h"
class ZIPPacker : public RefCounted {
GDCLASS(ZIPPacker, RefCounted);
Ref<FileAccess> fa;
zipFile zf = nullptr;
int compression_level = Z_DEFAULT_COMPRESSION;
protected:
static void _bind_methods();
public:
enum ZipAppend {
APPEND_CREATE = 0,
APPEND_CREATEAFTER = 1,
APPEND_ADDINZIP = 2,
};
enum CompressionLevel {
COMPRESSION_DEFAULT = Z_DEFAULT_COMPRESSION,
COMPRESSION_NONE = Z_NO_COMPRESSION,
COMPRESSION_FAST = Z_BEST_SPEED,
COMPRESSION_BEST = Z_BEST_COMPRESSION,
};
Error open(const String &p_path, ZipAppend p_append);
Error close();
void set_compression_level(int p_compression_level);
int get_compression_level() const;
Error start_file(const String &p_path);
Error write_file(const Vector<uint8_t> &p_data);
Error close_file();
ZIPPacker();
~ZIPPacker();
};
VARIANT_ENUM_CAST(ZIPPacker::ZipAppend)
VARIANT_ENUM_CAST(ZIPPacker::CompressionLevel)

177
modules/zip/zip_reader.cpp Normal file
View File

@@ -0,0 +1,177 @@
/**************************************************************************/
/* zip_reader.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. */
/**************************************************************************/
#include "zip_reader.h"
#include "core/error/error_macros.h"
#include "core/io/zip_io.h"
Error ZIPReader::open(const String &p_path) {
if (fa.is_valid()) {
close();
}
zlib_filefunc_def io = zipio_create_io(&fa);
uzf = unzOpen2(p_path.utf8().get_data(), &io);
return uzf != nullptr ? OK : FAILED;
}
Error ZIPReader::close() {
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPReader cannot be closed because it is not open.");
Error err = unzClose(uzf) == UNZ_OK ? OK : FAILED;
if (err == OK) {
DEV_ASSERT(fa.is_null());
uzf = nullptr;
}
return err;
}
PackedStringArray ZIPReader::get_files() {
ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), "ZIPReader must be opened before use.");
unz_global_info gi;
int err = unzGetGlobalInfo(uzf, &gi);
ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
if (gi.number_entry == 0) {
return PackedStringArray();
}
err = unzGoToFirstFile(uzf);
ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
List<String> s;
do {
unz_file_info64 file_info;
String filepath;
err = godot_unzip_get_current_file_info(uzf, file_info, filepath);
if (err == UNZ_OK) {
s.push_back(filepath);
}
} while (unzGoToNextFile(uzf) == UNZ_OK);
PackedStringArray arr;
arr.resize(s.size());
int idx = 0;
for (const List<String>::Element *E = s.front(); E; E = E->next()) {
arr.set(idx++, E->get());
}
return arr;
}
PackedByteArray ZIPReader::read_file(const String &p_path, bool p_case_sensitive) {
ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use.");
int err = UNZ_OK;
// Locate and open the file.
err = godot_unzip_locate_file(uzf, p_path, p_case_sensitive);
ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "File does not exist in zip archive: " + p_path);
err = unzOpenCurrentFile(uzf);
ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Could not open file within zip archive.");
// Read the file info.
unz_file_info info;
err = unzGetCurrentFileInfo(uzf, &info, nullptr, 0, nullptr, 0, nullptr, 0);
ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Unable to read file information from zip archive.");
ERR_FAIL_COND_V_MSG(info.uncompressed_size > INT_MAX, PackedByteArray(), "File contents too large to read from zip archive (>2 GB).");
// Read the file data.
PackedByteArray data;
data.resize(info.uncompressed_size);
uint8_t *buffer = data.ptrw();
int to_read = data.size();
while (to_read > 0) {
int bytes_read = unzReadCurrentFile(uzf, buffer, to_read);
ERR_FAIL_COND_V_MSG(bytes_read < 0, PackedByteArray(), "IO/zlib error reading file from zip archive.");
ERR_FAIL_COND_V_MSG(bytes_read == UNZ_EOF && to_read != 0, PackedByteArray(), "Incomplete file read from zip archive.");
DEV_ASSERT(bytes_read <= to_read);
buffer += bytes_read;
to_read -= bytes_read;
}
// Verify the data and return.
err = unzCloseCurrentFile(uzf);
ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "CRC error reading file from zip archive.");
return data;
}
bool ZIPReader::file_exists(const String &p_path, bool p_case_sensitive) {
ERR_FAIL_COND_V_MSG(fa.is_null(), false, "ZIPReader must be opened before use.");
int cs = p_case_sensitive ? 1 : 2;
if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
return false;
}
if (unzOpenCurrentFile(uzf) != UNZ_OK) {
return false;
}
unzCloseCurrentFile(uzf);
return true;
}
int ZIPReader::get_compression_level(const String &p_path, bool p_case_sensitive) {
ERR_FAIL_COND_V_MSG(fa.is_null(), -1, "ZIPReader must be opened before use.");
int cs = p_case_sensitive ? 1 : 2;
if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
return -1;
}
int method;
int level;
if (unzOpenCurrentFile2(uzf, &method, &level, 1) != UNZ_OK) {
return -1;
}
unzCloseCurrentFile(uzf);
return level;
}
ZIPReader::ZIPReader() {}
ZIPReader::~ZIPReader() {
if (fa.is_valid()) {
close();
}
}
void ZIPReader::_bind_methods() {
ClassDB::bind_method(D_METHOD("open", "path"), &ZIPReader::open);
ClassDB::bind_method(D_METHOD("close"), &ZIPReader::close);
ClassDB::bind_method(D_METHOD("get_files"), &ZIPReader::get_files);
ClassDB::bind_method(D_METHOD("read_file", "path", "case_sensitive"), &ZIPReader::read_file, DEFVAL(Variant(true)));
ClassDB::bind_method(D_METHOD("file_exists", "path", "case_sensitive"), &ZIPReader::file_exists, DEFVAL(Variant(true)));
ClassDB::bind_method(D_METHOD("get_compression_level", "path", "case_sensitive"), &ZIPReader::get_compression_level, DEFVAL(Variant(true)));
}

58
modules/zip/zip_reader.h Normal file
View File

@@ -0,0 +1,58 @@
/**************************************************************************/
/* zip_reader.h */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#pragma once
#include "core/io/file_access.h"
#include "core/object/ref_counted.h"
#include "thirdparty/minizip/unzip.h"
class ZIPReader : public RefCounted {
GDCLASS(ZIPReader, RefCounted)
Ref<FileAccess> fa;
unzFile uzf = nullptr;
protected:
static void _bind_methods();
public:
Error open(const String &p_path);
Error close();
PackedStringArray get_files();
PackedByteArray read_file(const String &p_path, bool p_case_sensitive);
bool file_exists(const String &p_path, bool p_case_sensitive);
int get_compression_level(const String &p_path, bool p_case_sensitive);
ZIPReader();
~ZIPReader();
};