|
@@ -40,38 +40,35 @@ Error ZIPReader::open(String p_path) {
|
|
|
|
|
|
zlib_filefunc_def io = zipio_create_io(&fa);
|
|
|
uzf = unzOpen2(p_path.utf8().get_data(), &io);
|
|
|
- return uzf != NULL ? OK : FAILED;
|
|
|
+ return uzf != nullptr ? OK : FAILED;
|
|
|
}
|
|
|
|
|
|
Error ZIPReader::close() {
|
|
|
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPReader cannot be closed because it is not open.");
|
|
|
|
|
|
- return unzClose(uzf) == UNZ_OK ? OK : FAILED;
|
|
|
+ Error err = unzClose(uzf) == UNZ_OK ? OK : FAILED;
|
|
|
+ if (err == OK) {
|
|
|
+ DEV_ASSERT(fa == nullptr);
|
|
|
+ uzf = nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return err;
|
|
|
}
|
|
|
|
|
|
PackedStringArray ZIPReader::get_files() {
|
|
|
ERR_FAIL_COND_V_MSG(fa.is_null(), PackedStringArray(), "ZIPReader must be opened before use.");
|
|
|
|
|
|
- List<String> s;
|
|
|
-
|
|
|
- if (unzGoToFirstFile(uzf) != UNZ_OK) {
|
|
|
- return PackedStringArray();
|
|
|
- }
|
|
|
+ int err = unzGoToFirstFile(uzf);
|
|
|
+ ERR_FAIL_COND_V(err != UNZ_OK, PackedStringArray());
|
|
|
|
|
|
+ List<String> s;
|
|
|
do {
|
|
|
unz_file_info64 file_info;
|
|
|
- char filename[256]; // Note filename is a path !
|
|
|
- int err = unzGetCurrentFileInfo64(uzf, &file_info, filename, sizeof(filename), NULL, 0, NULL, 0);
|
|
|
+ String filepath;
|
|
|
+
|
|
|
+ err = godot_unzip_get_current_file_info(uzf, file_info, filepath);
|
|
|
if (err == UNZ_OK) {
|
|
|
- s.push_back(filename);
|
|
|
- } else {
|
|
|
- // Assume filename buffer was too small
|
|
|
- char *long_filename_buff = (char *)memalloc(file_info.size_filename);
|
|
|
- int err2 = unzGetCurrentFileInfo64(uzf, NULL, long_filename_buff, sizeof(long_filename_buff), NULL, 0, NULL, 0);
|
|
|
- if (err2 == UNZ_OK) {
|
|
|
- s.push_back(long_filename_buff);
|
|
|
- memfree(long_filename_buff);
|
|
|
- }
|
|
|
+ s.push_back(filepath);
|
|
|
}
|
|
|
} while (unzGoToNextFile(uzf) == UNZ_OK);
|
|
|
|
|
@@ -87,23 +84,37 @@ PackedStringArray ZIPReader::get_files() {
|
|
|
PackedByteArray ZIPReader::read_file(String p_path, bool p_case_sensitive) {
|
|
|
ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "ZIPReader must be opened before use.");
|
|
|
|
|
|
- int cs = p_case_sensitive ? 1 : 2;
|
|
|
- if (unzLocateFile(uzf, p_path.utf8().get_data(), cs) != UNZ_OK) {
|
|
|
- ERR_FAIL_V_MSG(PackedByteArray(), "File does not exist in zip archive: " + p_path);
|
|
|
- }
|
|
|
- if (unzOpenCurrentFile(uzf) != UNZ_OK) {
|
|
|
- ERR_FAIL_V_MSG(PackedByteArray(), "Could not open file within zip archive.");
|
|
|
- }
|
|
|
+ int err = UNZ_OK;
|
|
|
+
|
|
|
+ // Locate and open the file.
|
|
|
+ err = godot_unzip_locate_file(uzf, p_path, p_case_sensitive);
|
|
|
+ ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "File does not exist in zip archive: " + p_path);
|
|
|
+ err = unzOpenCurrentFile(uzf);
|
|
|
+ ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Could not open file within zip archive.");
|
|
|
|
|
|
+ // Read the file info.
|
|
|
unz_file_info info;
|
|
|
- unzGetCurrentFileInfo(uzf, &info, NULL, 0, NULL, 0, NULL, 0);
|
|
|
+ err = unzGetCurrentFileInfo(uzf, &info, nullptr, 0, nullptr, 0, nullptr, 0);
|
|
|
+ ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "Unable to read file information from zip archive.");
|
|
|
+ ERR_FAIL_COND_V_MSG(info.uncompressed_size > INT_MAX, PackedByteArray(), "File contents too large to read from zip archive (>2 GB).");
|
|
|
+
|
|
|
+ // Read the file data.
|
|
|
PackedByteArray data;
|
|
|
data.resize(info.uncompressed_size);
|
|
|
+ uint8_t *buffer = data.ptrw();
|
|
|
+ int to_read = data.size();
|
|
|
+ while (to_read > 0) {
|
|
|
+ int bytes_read = unzReadCurrentFile(uzf, buffer, to_read);
|
|
|
+ ERR_FAIL_COND_V_MSG(bytes_read < 0, PackedByteArray(), "IO/zlib error reading file from zip archive.");
|
|
|
+ ERR_FAIL_COND_V_MSG(bytes_read == UNZ_EOF && to_read != 0, PackedByteArray(), "Incomplete file read from zip archive.");
|
|
|
+ DEV_ASSERT(bytes_read <= to_read);
|
|
|
+ buffer += bytes_read;
|
|
|
+ to_read -= bytes_read;
|
|
|
+ }
|
|
|
|
|
|
- uint8_t *w = data.ptrw();
|
|
|
- unzReadCurrentFile(uzf, &w[0], info.uncompressed_size);
|
|
|
-
|
|
|
- unzCloseCurrentFile(uzf);
|
|
|
+ // Verify the data and return.
|
|
|
+ err = unzCloseCurrentFile(uzf);
|
|
|
+ ERR_FAIL_COND_V_MSG(err != UNZ_OK, PackedByteArray(), "CRC error reading file from zip archive.");
|
|
|
return data;
|
|
|
}
|
|
|
|