Skip to content

Commit 7ed7d19

Browse files
committed
Merge pull request godotengine#76860 from AThousandShips/zip_file_exists
Add function `ZIPReader::file_exists`
2 parents 69cc7d3 + c31df54 commit 7ed7d19

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

modules/zip/doc_classes/ZIPReader.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
Closes the underlying resources used by this instance.
2626
</description>
2727
</method>
28+
<method name="file_exists">
29+
<return type="bool" />
30+
<param index="0" name="path" type="String" />
31+
<param index="1" name="case_sensitive" type="bool" default="true" />
32+
<description>
33+
Returns [code]true[/code] if the file exists in the loaded zip archive.
34+
Must be called after [method open].
35+
</description>
36+
</method>
2837
<method name="get_files">
2938
<return type="PackedStringArray" />
3039
<description>

modules/zip/zip_reader.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ PackedByteArray ZIPReader::read_file(String p_path, bool p_case_sensitive) {
118118
return data;
119119
}
120120

121+
bool ZIPReader::file_exists(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+
121136
ZIPReader::ZIPReader() {}
122137

123138
ZIPReader::~ZIPReader() {
@@ -131,4 +146,5 @@ void ZIPReader::_bind_methods() {
131146
ClassDB::bind_method(D_METHOD("close"), &ZIPReader::close);
132147
ClassDB::bind_method(D_METHOD("get_files"), &ZIPReader::get_files);
133148
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)));
134150
}

modules/zip/zip_reader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ZIPReader : public RefCounted {
5151

5252
PackedStringArray get_files();
5353
PackedByteArray read_file(String p_path, bool p_case_sensitive);
54+
bool file_exists(String p_path, bool p_case_sensitive);
5455

5556
ZIPReader();
5657
~ZIPReader();

0 commit comments

Comments
 (0)