Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions core/io/zip_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ void *zipio_open(void *data, const char *p_fname, int mode) {
String fname;
fname.parse_utf8(p_fname);

int file_access_mode = 0;
if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
f = FileAccess::open(fname, FileAccess::WRITE);
} else {
f = 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;
}
f = FileAccess::open(fname, file_access_mode);

if (!f) {
return nullptr;
Expand Down
72 changes: 72 additions & 0 deletions doc/classes/ZIPPacker.xml
Original file line number Diff line number Diff line change
@@ -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="PoolByteArray" />
<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 changes: 52 additions & 0 deletions doc/classes/ZIPReader.xml
Original file line number Diff line number Diff line change
@@ -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 PoolByteArray()
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="PoolStringArray" />
<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="PoolByteArray" />
<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>
9 changes: 9 additions & 0 deletions modules/zip/SCsub
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions modules/zip/config.py
Original file line number Diff line number Diff line change
@@ -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"
43 changes: 43 additions & 0 deletions modules/zip/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*************************************************************************/
/* 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 "zip_packer.h"
#include "zip_reader.h"

void register_zip_types() {

ClassDB::register_class<ZIPReader>();
ClassDB::register_class<ZIPPacker>();
}

void unregister_zip_types() {
}
37 changes: 37 additions & 0 deletions modules/zip/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*************************************************************************/
/* 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

void register_zip_types();
void unregister_zip_types();

#endif // ZIP_REGISTER_TYPES_H
113 changes: 113 additions & 0 deletions modules/zip/zip_packer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*************************************************************************/
/* 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) {
close();
fa = NULL;
}

zlib_filefunc_def io = zipio_create_io_from_file(&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, 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, FAILED, "ZIPPacker must be opened before use.");

zip_fileinfo zipfi;

OS::Time time = OS::get_singleton()->get_time();
OS::Date date = OS::get_singleton()->get_date();

zipfi.tmz_date.tm_hour = time.hour;
zipfi.tmz_date.tm_mday = date.day;
zipfi.tmz_date.tm_min = time.min;
zipfi.tmz_date.tm_mon = date.month - 1;
zipfi.tmz_date.tm_sec = time.sec;
zipfi.tmz_date.tm_year = date.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, 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, 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() {
fa = NULL;
}

ZIPPacker::~ZIPPacker() {
if (fa) {
close();
fa = NULL;
}
}
Loading