0.2.0-dev Download ZIP
96 lines ·3.7 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="ZIPReader" inherits="RefCounted" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd"> |
| 3 | <brief_description> |
| 4 | Allows reading the content of a ZIP file. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | This class implements a reader that can extract the content of individual files inside a ZIP archive. See also [ZIPPacker]. |
| 8 | [codeblock] |
| 9 | # Read a single file from a ZIP archive. |
| 10 | func read_zip_file(): |
| 11 | var reader = ZIPReader.new() |
| 12 | var err = reader.open("user://archive.zip") |
| 13 | if err != OK: |
| 14 | return PackedByteArray() |
| 15 | var res = reader.read_file("hello.txt") |
| 16 | reader.close() |
| 17 | return res |
| 18 | |
| 19 | # Extract all files from a ZIP archive, preserving the directories within. |
| 20 | # This acts like the "Extract all" functionality from most archive managers. |
| 21 | func extract_all_from_zip(): |
| 22 | var reader = ZIPReader.new() |
| 23 | reader.open("res://archive.zip") |
| 24 | |
| 25 | # Destination directory for the extracted files (this folder must exist before extraction). |
| 26 | # Not all ZIP archives put everything in a single root folder, |
| 27 | # which means several files/folders may be created in `root_dir` after extraction. |
| 28 | var root_dir = DirAccess.open("user://") |
| 29 | |
| 30 | var files = reader.get_files() |
| 31 | for file_path in files: |
| 32 | # If the current entry is a directory. |
| 33 | if file_path.ends_with("/"): |
| 34 | root_dir.make_dir_recursive(file_path) |
| 35 | continue |
| 36 | |
| 37 | # Write file contents, creating folders automatically when needed. |
| 38 | # Not all ZIP archives are strictly ordered, so we need to do this in case |
| 39 | # the file entry comes before the folder entry. |
| 40 | root_dir.make_dir_recursive(root_dir.get_current_dir().path_join(file_path).get_base_dir()) |
| 41 | var file = FileAccess.open(root_dir.get_current_dir().path_join(file_path), FileAccess.WRITE) |
| 42 | var buffer = reader.read_file(file_path) |
| 43 | file.store_buffer(buffer) |
| 44 | [/codeblock] |
| 45 | </description> |
| 46 | <tutorials> |
| 47 | </tutorials> |
| 48 | <methods> |
| 49 | <method name="close"> |
| 50 | <return type="int" enum="Error" /> |
| 51 | <description> |
| 52 | Closes the underlying resources used by this instance. |
| 53 | </description> |
| 54 | </method> |
| 55 | <method name="file_exists"> |
| 56 | <return type="bool" /> |
| 57 | <param index="0" name="path" type="String" /> |
| 58 | <param index="1" name="case_sensitive" type="bool" default="true" /> |
| 59 | <description> |
| 60 | Returns [code]true[/code] if the file exists in the loaded zip archive. |
| 61 | Must be called after [method open]. |
| 62 | </description> |
| 63 | </method> |
| 64 | <method name="get_compression_level"> |
| 65 | <return type="int" /> |
| 66 | <param index="0" name="path" type="String" /> |
| 67 | <param index="1" name="case_sensitive" type="bool" default="true" /> |
| 68 | <description> |
| 69 | Returns the compression level of the file in the loaded zip archive. Returns [code]-1[/code] if the file doesn't exist or any other error occurs. Must be called after [method open]. |
| 70 | </description> |
| 71 | </method> |
| 72 | <method name="get_files"> |
| 73 | <return type="PackedStringArray" /> |
| 74 | <description> |
| 75 | Returns the list of names of all files in the loaded archive. |
| 76 | Must be called after [method open]. |
| 77 | </description> |
| 78 | </method> |
| 79 | <method name="open"> |
| 80 | <return type="int" enum="Error" /> |
| 81 | <param index="0" name="path" type="String" /> |
| 82 | <description> |
| 83 | Opens the zip archive at the given [param path] and reads its file index. |
| 84 | </description> |
| 85 | </method> |
| 86 | <method name="read_file"> |
| 87 | <return type="PackedByteArray" /> |
| 88 | <param index="0" name="path" type="String" /> |
| 89 | <param index="1" name="case_sensitive" type="bool" default="true" /> |
| 90 | <description> |
| 91 | Loads the whole content of a file in the loaded zip archive into memory and returns it. |
| 92 | Must be called after [method open]. |
| 93 | </description> |
| 94 | </method> |
| 95 | </methods> |
| 96 | </class> |