MCPcopy Create free account
hub / github.com/Redot-Engine/redot-engine / read_file

Method read_file

modules/zip/zip_reader.cpp:99–134  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

97}
98
99PackedByteArray 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
136bool 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.");

Callers 1

test_zip.hFile · 0.80

Calls 5

godot_unzip_locate_fileFunction · 0.85
sizeMethod · 0.65
is_nullMethod · 0.45
resizeMethod · 0.45
ptrwMethod · 0.45

Tested by

no test coverage detected