Path: blob/master/editor/export/project_zip_packer.cpp
9903 views
/**************************************************************************/1/* project_zip_packer.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "project_zip_packer.h"3132#include "core/config/project_settings.h"33#include "core/io/dir_access.h"34#include "core/io/file_access.h"35#include "core/os/os.h"36#include "core/os/time.h"3738String ProjectZIPPacker::get_project_zip_safe_name() {39// Name the downloaded ZIP file to contain the project name and download date for easier organization.40// Replace characters not allowed (or risky) in Windows file names with safe characters.41// In the project name, all invalid characters become an empty string so that a name42// like "Platformer 2: Godette's Revenge" becomes "platformer_2-_godette-s_revenge".43const String project_name = GLOBAL_GET("application/config/name");44const String project_name_safe = project_name.to_lower().replace_char(' ', '_');45const String datetime_safe =46Time::get_singleton()->get_datetime_string_from_system(false, true).replace_char(' ', '_');47const String output_name = OS::get_singleton()->get_safe_dir_name(vformat("%s_%s.zip", project_name_safe, datetime_safe));48return output_name;49}5051void ProjectZIPPacker::pack_project_zip(const String &p_path) {52Ref<FileAccess> io_fa;53zlib_filefunc_def io = zipio_create_io(&io_fa);5455String resource_path = ProjectSettings::get_singleton()->get_resource_path();56const String base_path = resource_path.substr(0, resource_path.rfind_char('/')) + "/";5758zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);59_zip_recursive(resource_path, base_path, zip);60zipClose(zip, nullptr);61}6263void ProjectZIPPacker::_zip_file(const String &p_path, const String &p_base_path, zipFile p_zip) {64Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);65if (f.is_null()) {66WARN_PRINT("Unable to open file for zipping: " + p_path);67return;68}69Vector<uint8_t> data;70uint64_t len = f->get_length();71data.resize(len);72f->get_buffer(data.ptrw(), len);7374String path = p_path.replace_first(p_base_path, "");75zipOpenNewFileInZip(p_zip,76path.utf8().get_data(),77nullptr,78nullptr,790,80nullptr,810,82nullptr,83Z_DEFLATED,84Z_DEFAULT_COMPRESSION);85zipWriteInFileInZip(p_zip, data.ptr(), data.size());86zipCloseFileInZip(p_zip);87}8889void ProjectZIPPacker::_zip_recursive(const String &p_path, const String &p_base_path, zipFile p_zip) {90Ref<DirAccess> dir = DirAccess::open(p_path);91if (dir.is_null()) {92WARN_PRINT("Unable to open directory for zipping: " + p_path);93return;94}95dir->list_dir_begin();96String cur = dir->get_next();97String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();98while (!cur.is_empty()) {99String cs = p_path.path_join(cur);100if (cur == "." || cur == ".." || cur == project_data_dir_name) {101// Skip102} else if (dir->current_is_dir()) {103String path = cs.replace_first(p_base_path, "") + "/";104zipOpenNewFileInZip(p_zip,105path.utf8().get_data(),106nullptr,107nullptr,1080,109nullptr,1100,111nullptr,112Z_DEFLATED,113Z_DEFAULT_COMPRESSION);114zipCloseFileInZip(p_zip);115_zip_recursive(cs, p_base_path, p_zip);116} else {117_zip_file(cs, p_base_path, p_zip);118}119cur = dir->get_next();120}121}122123124