ZIPReader.xml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="ZIPReader" inherits="RefCounted" 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. # Extract all files from a ZIP archive, preserving the directories within.
  19. # This acts like the "Extract all" functionality from most archive managers.
  20. func extract_all_from_zip():
  21. var reader = ZIPReader.new()
  22. reader.open("res://archive.zip")
  23. # Destination directory for the extracted files (this folder must exist before extraction).
  24. # Not all ZIP archives put everything in a single root folder,
  25. # which means several files/folders may be created in `root_dir` after extraction.
  26. var root_dir = DirAccess.open("user://")
  27. var files = reader.get_files()
  28. for file_path in files:
  29. # If the current entry is a directory.
  30. if file_path.ends_with("/"):
  31. root_dir.make_dir_recursive(file_path)
  32. continue
  33. # Write file contents, creating folders automatically when needed.
  34. # Not all ZIP archives are strictly ordered, so we need to do this in case
  35. # the file entry comes before the folder entry.
  36. root_dir.make_dir_recursive(root_dir.get_current_dir().path_join(file_path).get_base_dir())
  37. var file = FileAccess.open(root_dir.get_current_dir().path_join(file_path), FileAccess.WRITE)
  38. var buffer = reader.read_file(file_path)
  39. file.store_buffer(buffer)
  40. [/codeblock]
  41. </description>
  42. <tutorials>
  43. </tutorials>
  44. <methods>
  45. <method name="close">
  46. <return type="int" enum="Error" />
  47. <description>
  48. Closes the underlying resources used by this instance.
  49. </description>
  50. </method>
  51. <method name="file_exists">
  52. <return type="bool" />
  53. <param index="0" name="path" type="String" />
  54. <param index="1" name="case_sensitive" type="bool" default="true" />
  55. <description>
  56. Returns [code]true[/code] if the file exists in the loaded zip archive.
  57. Must be called after [method open].
  58. </description>
  59. </method>
  60. <method name="get_compression_level">
  61. <return type="int" />
  62. <param index="0" name="path" type="String" />
  63. <param index="1" name="case_sensitive" type="bool" default="true" />
  64. <description>
  65. 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].
  66. </description>
  67. </method>
  68. <method name="get_files">
  69. <return type="PackedStringArray" />
  70. <description>
  71. Returns the list of names of all files in the loaded archive.
  72. Must be called after [method open].
  73. </description>
  74. </method>
  75. <method name="open">
  76. <return type="int" enum="Error" />
  77. <param index="0" name="path" type="String" />
  78. <description>
  79. Opens the zip archive at the given [param path] and reads its file index.
  80. </description>
  81. </method>
  82. <method name="read_file">
  83. <return type="PackedByteArray" />
  84. <param index="0" name="path" type="String" />
  85. <param index="1" name="case_sensitive" type="bool" default="true" />
  86. <description>
  87. Loads the whole content of a file in the loaded zip archive into memory and returns it.
  88. Must be called after [method open].
  89. </description>
  90. </method>
  91. </methods>
  92. </class>