| 97 | } |
| 98 | |
| 99 | PackedByteArray ZIPReader::read_file(const String &p_path, bool p_case_sensitive) { |
| 100 | ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use."); |
| 101 | |
| 102 | int err = UNZ_OK; |
| 103 | |
| 104 | // Locate and open the file. |
| 105 | err = godot_unzip_locate_file(uzf, p_path, p_case_sensitive); |
| 106 | ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "File does not exist in zip archive: " + p_path); |
| 107 | err = unzOpenCurrentFile(uzf); |
| 108 | ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Could not open file within zip archive."); |
| 109 | |
| 110 | // Read the file info. |
| 111 | unz_file_info info; |
| 112 | err = unzGetCurrentFileInfo(uzf, &info, nullptr, 0, nullptr, 0, nullptr, 0); |
| 113 | ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Unable to read file information from zip archive."); |
| 114 | ERR_FAIL_COND_V_MSG(info.uncompressed_size > INT_MAX, PackedByteArray(), "File contents too large to read from zip archive (>2 GB)."); |
| 115 | |
| 116 | // Read the file data. |
| 117 | PackedByteArray data; |
| 118 | data.resize(info.uncompressed_size); |
| 119 | uint8_t *buffer = data.ptrw(); |
| 120 | int to_read = data.size(); |
| 121 | while (to_read > 0) { |
| 122 | int bytes_read = unzReadCurrentFile(uzf, buffer, to_read); |
| 123 | ERR_FAIL_COND_V_MSG(bytes_read < 0, PackedByteArray(), "IO/zlib error reading file from zip archive."); |
| 124 | ERR_FAIL_COND_V_MSG(bytes_read == UNZ_EOF && to_read != 0, PackedByteArray(), "Incomplete file read from zip archive."); |
| 125 | DEV_ASSERT(bytes_read <= to_read); |
| 126 | buffer += bytes_read; |
| 127 | to_read -= bytes_read; |
| 128 | } |
| 129 | |
| 130 | // Verify the data and return. |
| 131 | err = unzCloseCurrentFile(uzf); |
| 132 | ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "CRC error reading file from zip archive."); |
| 133 | return data; |
| 134 | } |
| 135 | |
| 136 | bool ZIPReader::file_exists(const String &p_path, bool p_case_sensitive) { |
| 137 | ERR_FAIL_COND_V_MSG(fa.is_null(), false, "ZIPReader must be opened before use."); |
no test coverage detected