Merge pull request #65281 from MisterMX/zip-module
Expose minizip API to allow creating zips using scripts
This commit is contained in:
commit
96ec73f3ee
11 changed files with 606 additions and 3 deletions
|
@ -37,11 +37,17 @@ void *zipio_open(voidpf opaque, const char *p_fname, int mode) {
|
|||
String fname;
|
||||
fname.parse_utf8(p_fname);
|
||||
|
||||
int file_access_mode = 0;
|
||||
if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
|
||||
(*fa) = FileAccess::open(fname, FileAccess::WRITE);
|
||||
} else {
|
||||
(*fa) = FileAccess::open(fname, FileAccess::READ);
|
||||
file_access_mode |= FileAccess::WRITE;
|
||||
}
|
||||
if (mode & ZLIB_FILEFUNC_MODE_READ) {
|
||||
file_access_mode |= FileAccess::READ;
|
||||
}
|
||||
if (mode & ZLIB_FILEFUNC_MODE_CREATE) {
|
||||
file_access_mode |= FileAccess::WRITE_READ;
|
||||
}
|
||||
(*fa) = FileAccess::open(fname, file_access_mode);
|
||||
|
||||
if (fa->is_null()) {
|
||||
return nullptr;
|
||||
|
|
9
modules/zip/SCsub
Normal file
9
modules/zip/SCsub
Normal file
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
Import("env")
|
||||
Import("env_modules")
|
||||
|
||||
env_zip = env_modules.Clone()
|
||||
|
||||
# Module files
|
||||
env_zip.add_source_files(env.modules_sources, "*.cpp")
|
17
modules/zip/config.py
Normal file
17
modules/zip/config.py
Normal 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"
|
72
modules/zip/doc_classes/ZIPPacker.xml
Normal file
72
modules/zip/doc_classes/ZIPPacker.xml
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="ZIPPacker" inherits="RefCounted" version="4.0" 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.
|
||||
[codeblock]
|
||||
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>
|
||||
<constants>
|
||||
<constant name="APPEND_CREATE" value="0" enum="ZipAppend">
|
||||
</constant>
|
||||
<constant name="APPEND_CREATEAFTER" value="1" enum="ZipAppend">
|
||||
</constant>
|
||||
<constant name="APPEND_ADDINZIP" value="2" enum="ZipAppend">
|
||||
</constant>
|
||||
</constants>
|
||||
</class>
|
52
modules/zip/doc_classes/ZIPReader.xml
Normal file
52
modules/zip/doc_classes/ZIPReader.xml
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="ZIPReader" inherits="RefCounted" version="4.0" 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.
|
||||
[codeblock]
|
||||
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
|
||||
[/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="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>
|
50
modules/zip/register_types.cpp
Normal file
50
modules/zip/register_types.cpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*************************************************************************/
|
||||
/* register_types.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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 "core/object/class_db.h"
|
||||
#include "zip_packer.h"
|
||||
#include "zip_reader.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;
|
||||
}
|
||||
}
|
39
modules/zip/register_types.h
Normal file
39
modules/zip/register_types.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*************************************************************************/
|
||||
/* register_types.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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 ZIP_REGISTER_TYPES_H
|
||||
#define ZIP_REGISTER_TYPES_H
|
||||
|
||||
#include "modules/register_module_types.h"
|
||||
|
||||
void initialize_zip_module(ModuleInitializationLevel p_level);
|
||||
void uninitialize_zip_module(ModuleInitializationLevel p_level);
|
||||
|
||||
#endif // ZIP_REGISTER_TYPES_H
|
108
modules/zip/zip_packer.cpp
Normal file
108
modules/zip/zip_packer.cpp
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*************************************************************************/
|
||||
/* zip_packer.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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(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, NULL, &io);
|
||||
return zf != NULL ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPPacker::close() {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker cannot be closed because it is not open.");
|
||||
|
||||
return zipClose(zf, NULL) == ZIP_OK ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPPacker::start_file(String p_path) {
|
||||
ERR_FAIL_COND_V_MSG(zf != NULL, FAILED, "ZIPPacker is already in use.");
|
||||
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_hour = time.hour;
|
||||
zipfi.tmz_date.tm_mday = time.day;
|
||||
zipfi.tmz_date.tm_min = time.minute;
|
||||
zipfi.tmz_date.tm_mon = time.month - 1;
|
||||
zipfi.tmz_date.tm_sec = time.second;
|
||||
zipfi.tmz_date.tm_year = time.year;
|
||||
zipfi.dosDate = 0;
|
||||
zipfi.external_fa = 0;
|
||||
zipfi.internal_fa = 0;
|
||||
|
||||
int ret = zipOpenNewFileInZip(zf, p_path.utf8().get_data(), &zipfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
|
||||
return ret == ZIP_OK ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPPacker::write_file(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.");
|
||||
|
||||
Error err = zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
|
||||
if (err == OK) {
|
||||
zf = NULL;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
68
modules/zip/zip_packer.h
Normal file
68
modules/zip/zip_packer.h
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*************************************************************************/
|
||||
/* zip_packer.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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 ZIP_PACKER_H
|
||||
#define ZIP_PACKER_H
|
||||
|
||||
#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;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
enum ZipAppend {
|
||||
APPEND_CREATE = 0,
|
||||
APPEND_CREATEAFTER = 1,
|
||||
APPEND_ADDINZIP = 2,
|
||||
};
|
||||
|
||||
Error open(String p_path, ZipAppend p_append);
|
||||
Error close();
|
||||
|
||||
Error start_file(String p_path);
|
||||
Error write_file(Vector<uint8_t> p_data);
|
||||
Error close_file();
|
||||
|
||||
ZIPPacker();
|
||||
~ZIPPacker();
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(ZIPPacker::ZipAppend)
|
||||
|
||||
#endif // ZIP_PACKER_H
|
123
modules/zip/zip_reader.cpp
Normal file
123
modules/zip/zip_reader.cpp
Normal file
|
@ -0,0 +1,123 @@
|
|||
/*************************************************************************/
|
||||
/* zip_reader.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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(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 != NULL ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPReader::close() {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPReader cannot be closed because it is not open.");
|
||||
|
||||
return unzClose(uzf) == UNZ_OK ? OK : FAILED;
|
||||
}
|
||||
|
||||
PackedStringArray ZIPReader::get_files() {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), "ZIPReader must be opened before use.");
|
||||
|
||||
List<String> s;
|
||||
|
||||
if (unzGoToFirstFile(uzf) != UNZ_OK) {
|
||||
return PackedStringArray();
|
||||
}
|
||||
|
||||
do {
|
||||
unz_file_info64 file_info;
|
||||
char filename[256]; // Note filename is a path !
|
||||
int err = unzGetCurrentFileInfo64(uzf, &file_info, filename, sizeof(filename), NULL, 0, NULL, 0);
|
||||
if (err == UNZ_OK) {
|
||||
s.push_back(filename);
|
||||
} else {
|
||||
// Assume filename buffer was too small
|
||||
char *long_filename_buff = (char *)memalloc(file_info.size_filename);
|
||||
int err2 = unzGetCurrentFileInfo64(uzf, NULL, long_filename_buff, sizeof(long_filename_buff), NULL, 0, NULL, 0);
|
||||
if (err2 == UNZ_OK) {
|
||||
s.push_back(long_filename_buff);
|
||||
memfree(long_filename_buff);
|
||||
}
|
||||
}
|
||||
} 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(String p_path, bool p_case_sensitive) {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use.");
|
||||
|
||||
int cs = p_case_sensitive ? 1 : 2;
|
||||
if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
|
||||
ERR_FAIL_V_MSG(PackedByteArray(), "File does not exist in zip archive: " + p_path);
|
||||
}
|
||||
if (unzOpenCurrentFile(uzf) != UNZ_OK) {
|
||||
ERR_FAIL_V_MSG(PackedByteArray(), "Could not open file within zip archive.");
|
||||
}
|
||||
|
||||
unz_file_info info;
|
||||
unzGetCurrentFileInfo(uzf, &info, NULL, 0, NULL, 0, NULL, 0);
|
||||
PackedByteArray data;
|
||||
data.resize(info.uncompressed_size);
|
||||
|
||||
uint8_t *w = data.ptrw();
|
||||
unzReadCurrentFile(uzf, &w[0], info.uncompressed_size);
|
||||
|
||||
unzCloseCurrentFile(uzf);
|
||||
return data;
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
59
modules/zip/zip_reader.h
Normal file
59
modules/zip/zip_reader.h
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*************************************************************************/
|
||||
/* zip_reader.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
||||
/* */
|
||||
/* 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 ZIP_READER_H
|
||||
#define ZIP_READER_H
|
||||
|
||||
#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;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
Error open(String p_path);
|
||||
Error close();
|
||||
|
||||
PackedStringArray get_files();
|
||||
PackedByteArray read_file(String p_path, bool p_case_sensitive);
|
||||
|
||||
ZIPReader();
|
||||
~ZIPReader();
|
||||
};
|
||||
|
||||
#endif // ZIP_READER_H
|
Loading…
Reference in a new issue