class_zipreader.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/modules/zip/doc_classes/ZIPReader.xml.
  6. .. _class_ZIPReader:
  7. ZIPReader
  8. =========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Allows reading the content of a ZIP file.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. This class implements a reader that can extract the content of individual files inside a ZIP archive. See also :ref:`ZIPPacker<class_ZIPPacker>`.
  15. ::
  16. # Read a single file from a ZIP archive.
  17. func read_zip_file():
  18. var reader = ZIPReader.new()
  19. var err = reader.open("user://archive.zip")
  20. if err != OK:
  21. return PackedByteArray()
  22. var res = reader.read_file("hello.txt")
  23. reader.close()
  24. return res
  25. # Extract all files from a ZIP archive, preserving the directories within.
  26. # This acts like the "Extract all" functionality from most archive managers.
  27. func extract_all_from_zip():
  28. var reader = ZIPReader.new()
  29. reader.open("res://archive.zip")
  30. # Destination directory for the extracted files (this folder must exist before extraction).
  31. # Not all ZIP archives put everything in a single root folder,
  32. # which means several files/folders may be created in `root_dir` after extraction.
  33. var root_dir = DirAccess.open("user://")
  34. var files = reader.get_files()
  35. for file_path in files:
  36. # If the current entry is a directory.
  37. if file_path.ends_with("/"):
  38. root_dir.make_dir_recursive(file_path)
  39. continue
  40. # Write file contents, creating folders automatically when needed.
  41. # Not all ZIP archives are strictly ordered, so we need to do this in case
  42. # the file entry comes before the folder entry.
  43. root_dir.make_dir_recursive(root_dir.get_current_dir().path_join(file_path).get_base_dir())
  44. var file = FileAccess.open(root_dir.get_current_dir().path_join(file_path), FileAccess.WRITE)
  45. var buffer = reader.read_file(file_path)
  46. file.store_buffer(buffer)
  47. .. rst-class:: classref-reftable-group
  48. Methods
  49. -------
  50. .. table::
  51. :widths: auto
  52. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`close<class_ZIPReader_method_close>`\ (\ ) |
  54. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`bool<class_bool>` | :ref:`file_exists<class_ZIPReader_method_file_exists>`\ (\ path\: :ref:`String<class_String>`, case_sensitive\: :ref:`bool<class_bool>` = true\ ) |
  56. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_files<class_ZIPReader_method_get_files>`\ (\ ) |
  58. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open<class_ZIPReader_method_open>`\ (\ path\: :ref:`String<class_String>`\ ) |
  60. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`read_file<class_ZIPReader_method_read_file>`\ (\ path\: :ref:`String<class_String>`, case_sensitive\: :ref:`bool<class_bool>` = true\ ) |
  62. +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  63. .. rst-class:: classref-section-separator
  64. ----
  65. .. rst-class:: classref-descriptions-group
  66. Method Descriptions
  67. -------------------
  68. .. _class_ZIPReader_method_close:
  69. .. rst-class:: classref-method
  70. :ref:`Error<enum_@GlobalScope_Error>` **close**\ (\ ) :ref:`🔗<class_ZIPReader_method_close>`
  71. Closes the underlying resources used by this instance.
  72. .. rst-class:: classref-item-separator
  73. ----
  74. .. _class_ZIPReader_method_file_exists:
  75. .. rst-class:: classref-method
  76. :ref:`bool<class_bool>` **file_exists**\ (\ path\: :ref:`String<class_String>`, case_sensitive\: :ref:`bool<class_bool>` = true\ ) :ref:`🔗<class_ZIPReader_method_file_exists>`
  77. Returns ``true`` if the file exists in the loaded zip archive.
  78. Must be called after :ref:`open()<class_ZIPReader_method_open>`.
  79. .. rst-class:: classref-item-separator
  80. ----
  81. .. _class_ZIPReader_method_get_files:
  82. .. rst-class:: classref-method
  83. :ref:`PackedStringArray<class_PackedStringArray>` **get_files**\ (\ ) :ref:`🔗<class_ZIPReader_method_get_files>`
  84. Returns the list of names of all files in the loaded archive.
  85. Must be called after :ref:`open()<class_ZIPReader_method_open>`.
  86. .. rst-class:: classref-item-separator
  87. ----
  88. .. _class_ZIPReader_method_open:
  89. .. rst-class:: classref-method
  90. :ref:`Error<enum_@GlobalScope_Error>` **open**\ (\ path\: :ref:`String<class_String>`\ ) :ref:`🔗<class_ZIPReader_method_open>`
  91. Opens the zip archive at the given ``path`` and reads its file index.
  92. .. rst-class:: classref-item-separator
  93. ----
  94. .. _class_ZIPReader_method_read_file:
  95. .. rst-class:: classref-method
  96. :ref:`PackedByteArray<class_PackedByteArray>` **read_file**\ (\ path\: :ref:`String<class_String>`, case_sensitive\: :ref:`bool<class_bool>` = true\ ) :ref:`🔗<class_ZIPReader_method_read_file>`
  97. Loads the whole content of a file in the loaded zip archive into memory and returns it.
  98. Must be called after :ref:`open()<class_ZIPReader_method_open>`.
  99. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  100. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  101. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  102. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  103. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  104. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  105. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  106. .. |void| replace:: :abbr:`void (No return value.)`