mirror of https://github.com/godotengine/godot
Add compression level support to ZipPacker
This commit is contained in:
parent
cc7a951140
commit
831b35803c
|
|
@ -178,6 +178,7 @@
|
||||||
/modules/regex/tests/ @godotengine/core @godotengine/tests
|
/modules/regex/tests/ @godotengine/core @godotengine/tests
|
||||||
/modules/zip/ @godotengine/core
|
/modules/zip/ @godotengine/core
|
||||||
/modules/zip/doc_classes/ @godotengine/core @godotengine/documentation
|
/modules/zip/doc_classes/ @godotengine/core @godotengine/documentation
|
||||||
|
/modules/zip/tests @godotengine/core @godotengine/tests
|
||||||
|
|
||||||
# Platform
|
# Platform
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,3 +8,7 @@ env_zip = env_modules.Clone()
|
||||||
|
|
||||||
# Module files
|
# Module files
|
||||||
env_zip.add_source_files(env.modules_sources, "*.cpp")
|
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")
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,13 @@
|
||||||
This must be called before everything else.
|
This must be called before everything else.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
|
<method name="set_compression_level">
|
||||||
|
<return type="void" />
|
||||||
|
<param index="0" name="compression_level" type="int" enum="ZIPPacker.CompressionLevel" />
|
||||||
|
<description>
|
||||||
|
Set the compression level used when [method start_file] is called.
|
||||||
|
</description>
|
||||||
|
</method>
|
||||||
<method name="start_file">
|
<method name="start_file">
|
||||||
<return type="int" enum="Error" />
|
<return type="int" enum="Error" />
|
||||||
<param index="0" name="path" type="String" />
|
<param index="0" name="path" type="String" />
|
||||||
|
|
@ -72,5 +79,17 @@
|
||||||
<constant name="APPEND_ADDINZIP" value="2" enum="ZipAppend">
|
<constant name="APPEND_ADDINZIP" value="2" enum="ZipAppend">
|
||||||
Add new files to the existing zip archive at the given path.
|
Add new files to the existing zip archive at the given path.
|
||||||
</constant>
|
</constant>
|
||||||
|
<constant name="COMPRESSION_NONE" value="1" enum="CompressionLevel">
|
||||||
|
Start a file with no compression.
|
||||||
|
</constant>
|
||||||
|
<constant name="COMPRESSION_DEFAULT" value="0" enum="CompressionLevel">
|
||||||
|
Start a file with the default compression level.
|
||||||
|
</constant>
|
||||||
|
<constant name="COMPRESSION_BEST" value="3" enum="CompressionLevel">
|
||||||
|
Start a file with the the best compression level.
|
||||||
|
</constant>
|
||||||
|
<constant name="COMPRESSION_BEST_SPEED" value="2" enum="CompressionLevel">
|
||||||
|
Start a file with the the best and fast compression level.
|
||||||
|
</constant>
|
||||||
</constants>
|
</constants>
|
||||||
</class>
|
</class>
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,102 @@
|
||||||
|
/**************************************************************************/
|
||||||
|
/* 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. */
|
||||||
|
/**************************************************************************/
|
||||||
|
|
||||||
|
#ifndef TEST_ZIP_H
|
||||||
|
#define TEST_ZIP_H
|
||||||
|
|
||||||
|
#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");
|
||||||
|
ZIPPacker packer;
|
||||||
|
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");
|
||||||
|
ZIPPacker packer;
|
||||||
|
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");
|
||||||
|
ZIPReader reader;
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace TestZip
|
||||||
|
|
||||||
|
#endif // TEST_ZIP_H
|
||||||
|
|
@ -55,6 +55,23 @@ Error ZIPPacker::close() {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ZIPPacker::set_compression_level(CompressionLevel p_compression_level) {
|
||||||
|
switch (p_compression_level) {
|
||||||
|
case COMPRESSION_NONE:
|
||||||
|
compression_level = Z_NO_COMPRESSION;
|
||||||
|
break;
|
||||||
|
case COMPRESSION_BEST_SPEED:
|
||||||
|
compression_level = Z_BEST_SPEED;
|
||||||
|
break;
|
||||||
|
case COMPRESSION_BEST:
|
||||||
|
compression_level = Z_BEST_COMPRESSION;
|
||||||
|
break;
|
||||||
|
case COMPRESSION_DEFAULT:
|
||||||
|
default:
|
||||||
|
compression_level = Z_DEFAULT_COMPRESSION;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Error ZIPPacker::start_file(const String &p_path) {
|
Error ZIPPacker::start_file(const String &p_path) {
|
||||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
||||||
|
|
||||||
|
|
@ -81,7 +98,7 @@ Error ZIPPacker::start_file(const String &p_path) {
|
||||||
0,
|
0,
|
||||||
nullptr,
|
nullptr,
|
||||||
Z_DEFLATED,
|
Z_DEFLATED,
|
||||||
Z_DEFAULT_COMPRESSION,
|
compression_level,
|
||||||
0,
|
0,
|
||||||
-MAX_WBITS,
|
-MAX_WBITS,
|
||||||
DEF_MEM_LEVEL,
|
DEF_MEM_LEVEL,
|
||||||
|
|
@ -107,6 +124,7 @@ Error ZIPPacker::close_file() {
|
||||||
|
|
||||||
void ZIPPacker::_bind_methods() {
|
void ZIPPacker::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
|
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("start_file", "path"), &ZIPPacker::start_file);
|
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("write_file", "data"), &ZIPPacker::write_file);
|
||||||
ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
|
ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
|
||||||
|
|
@ -115,9 +133,16 @@ void ZIPPacker::_bind_methods() {
|
||||||
BIND_ENUM_CONSTANT(APPEND_CREATE);
|
BIND_ENUM_CONSTANT(APPEND_CREATE);
|
||||||
BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
|
BIND_ENUM_CONSTANT(APPEND_CREATEAFTER);
|
||||||
BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
|
BIND_ENUM_CONSTANT(APPEND_ADDINZIP);
|
||||||
|
|
||||||
|
BIND_ENUM_CONSTANT(COMPRESSION_NONE);
|
||||||
|
BIND_ENUM_CONSTANT(COMPRESSION_DEFAULT);
|
||||||
|
BIND_ENUM_CONSTANT(COMPRESSION_BEST);
|
||||||
|
BIND_ENUM_CONSTANT(COMPRESSION_BEST_SPEED);
|
||||||
}
|
}
|
||||||
|
|
||||||
ZIPPacker::ZIPPacker() {}
|
ZIPPacker::ZIPPacker() {
|
||||||
|
compression_level = Z_DEFAULT_COMPRESSION;
|
||||||
|
}
|
||||||
|
|
||||||
ZIPPacker::~ZIPPacker() {
|
ZIPPacker::~ZIPPacker() {
|
||||||
if (fa.is_valid()) {
|
if (fa.is_valid()) {
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ class ZIPPacker : public RefCounted {
|
||||||
|
|
||||||
Ref<FileAccess> fa;
|
Ref<FileAccess> fa;
|
||||||
zipFile zf = nullptr;
|
zipFile zf = nullptr;
|
||||||
|
int compression_level;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
@ -52,9 +53,18 @@ public:
|
||||||
APPEND_ADDINZIP = 2,
|
APPEND_ADDINZIP = 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum CompressionLevel {
|
||||||
|
COMPRESSION_DEFAULT = 0,
|
||||||
|
COMPRESSION_NONE = 1,
|
||||||
|
COMPRESSION_BEST_SPEED = 2,
|
||||||
|
COMPRESSION_BEST = 3,
|
||||||
|
};
|
||||||
|
|
||||||
Error open(const String &p_path, ZipAppend p_append);
|
Error open(const String &p_path, ZipAppend p_append);
|
||||||
Error close();
|
Error close();
|
||||||
|
|
||||||
|
void set_compression_level(CompressionLevel p_compression_level);
|
||||||
|
|
||||||
Error start_file(const String &p_path);
|
Error start_file(const String &p_path);
|
||||||
Error write_file(const Vector<uint8_t> &p_data);
|
Error write_file(const Vector<uint8_t> &p_data);
|
||||||
Error close_file();
|
Error close_file();
|
||||||
|
|
@ -64,5 +74,6 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
VARIANT_ENUM_CAST(ZIPPacker::ZipAppend)
|
VARIANT_ENUM_CAST(ZIPPacker::ZipAppend)
|
||||||
|
VARIANT_ENUM_CAST(ZIPPacker::CompressionLevel)
|
||||||
|
|
||||||
#endif // ZIP_PACKER_H
|
#endif // ZIP_PACKER_H
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue