| 1 | /**************************************************************************/ |
| 2 | /* zip_reader.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "zip_reader.h" |
| 32 | |
| 33 | #include "core/error/error_macros.h" |
| 34 | #include "core/io/zip_io.h" |
| 35 | |
| 36 | Error ZIPReader::open(const String &p_path) { |
| 37 | if (fa.is_valid()) { |
| 38 | close(); |
| 39 | } |
| 40 | |
| 41 | zlib_filefunc_def io = zipio_create_io(&fa); |
| 42 | uzf = unzOpen2(p_path.utf8().get_data(), &io); |
| 43 | return uzf != nullptr ? OK : FAILED; |
| 44 | } |
| 45 | |
| 46 | Error ZIPReader::close() { |
| 47 | ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPReader cannot be closed because it is not open." ); |
| 48 | |
| 49 | Error err = unzClose(uzf) == UNZ_OK ? OK : FAILED; |
| 50 | if (err == OK) { |
| 51 | DEV_ASSERT(fa == nullptr); |
| 52 | uzf = nullptr; |
| 53 | } |
| 54 | |
| 55 | return err; |
| 56 | } |
| 57 | |
| 58 | PackedStringArray ZIPReader::get_files() { |
| 59 | ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), "ZIPReader must be opened before use." ); |
| 60 | |
| 61 | int err = unzGoToFirstFile(uzf); |
| 62 | ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray()); |
| 63 | |
| 64 | List<String> s; |
| 65 | do { |
| 66 | unz_file_info64 file_info; |
| 67 | String filepath; |
| 68 | |
| 69 | err = godot_unzip_get_current_file_info(uzf, file_info, filepath); |
| 70 | if (err == UNZ_OK) { |
| 71 | s.push_back(filepath); |
| 72 | } |
| 73 | } while (unzGoToNextFile(uzf) == UNZ_OK); |
| 74 | |
| 75 | PackedStringArray arr; |
| 76 | arr.resize(s.size()); |
| 77 | int idx = 0; |
| 78 | for (const List<String>::Element *E = s.front(); E; E = E->next()) { |
| 79 | arr.set(idx++, E->get()); |
| 80 | } |
| 81 | return arr; |
| 82 | } |
| 83 | |
| 84 | PackedByteArray ZIPReader::read_file(const String &p_path, bool p_case_sensitive) { |
| 85 | ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use." ); |
| 86 | |
| 87 | int err = UNZ_OK; |
| 88 | |
| 89 | // Locate and open the file. |
| 90 | err = godot_unzip_locate_file(uzf, p_path, p_case_sensitive); |
| 91 | ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "File does not exist in zip archive: " + p_path); |
| 92 | err = unzOpenCurrentFile(uzf); |
| 93 | ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Could not open file within zip archive." ); |
| 94 | |
| 95 | // Read the file info. |
| 96 | unz_file_info info; |
| 97 | err = unzGetCurrentFileInfo(uzf, &info, nullptr, 0, nullptr, 0, nullptr, 0); |
| 98 | ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Unable to read file information from zip archive." ); |
| 99 | ERR_FAIL_COND_V_MSG(info.uncompressed_size > INT_MAX, PackedByteArray(), "File contents too large to read from zip archive (>2 GB)." ); |
| 100 | |
| 101 | // Read the file data. |
| 102 | PackedByteArray data; |
| 103 | data.resize(info.uncompressed_size); |
| 104 | uint8_t *buffer = data.ptrw(); |
| 105 | int to_read = data.size(); |
| 106 | while (to_read > 0) { |
| 107 | int bytes_read = unzReadCurrentFile(uzf, buffer, to_read); |
| 108 | ERR_FAIL_COND_V_MSG(bytes_read < 0, PackedByteArray(), "IO/zlib error reading file from zip archive." ); |
| 109 | ERR_FAIL_COND_V_MSG(bytes_read == UNZ_EOF && to_read != 0, PackedByteArray(), "Incomplete file read from zip archive." ); |
| 110 | DEV_ASSERT(bytes_read <= to_read); |
| 111 | buffer += bytes_read; |
| 112 | to_read -= bytes_read; |
| 113 | } |
| 114 | |
| 115 | // Verify the data and return. |
| 116 | err = unzCloseCurrentFile(uzf); |
| 117 | ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "CRC error reading file from zip archive." ); |
| 118 | return data; |
| 119 | } |
| 120 | |
| 121 | bool ZIPReader::file_exists(const String &p_path, bool p_case_sensitive) { |
| 122 | ERR_FAIL_COND_V_MSG(fa.is_null(), false, "ZIPReader must be opened before use." ); |
| 123 | |
| 124 | int cs = p_case_sensitive ? 1 : 2; |
| 125 | if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) { |
| 126 | return false; |
| 127 | } |
| 128 | if (unzOpenCurrentFile(uzf) != UNZ_OK) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | unzCloseCurrentFile(uzf); |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | ZIPReader::ZIPReader() {} |
| 137 | |
| 138 | ZIPReader::~ZIPReader() { |
| 139 | if (fa.is_valid()) { |
| 140 | close(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void ZIPReader::_bind_methods() { |
| 145 | ClassDB::bind_method(D_METHOD("open" , "path" ), &ZIPReader::open); |
| 146 | ClassDB::bind_method(D_METHOD("close" ), &ZIPReader::close); |
| 147 | ClassDB::bind_method(D_METHOD("get_files" ), &ZIPReader::get_files); |
| 148 | ClassDB::bind_method(D_METHOD("read_file" , "path" , "case_sensitive" ), &ZIPReader::read_file, DEFVAL(Variant(true))); |
| 149 | ClassDB::bind_method(D_METHOD("file_exists" , "path" , "case_sensitive" ), &ZIPReader::file_exists, DEFVAL(Variant(true))); |
| 150 | } |
| 151 | |