Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to unzip .zip files #11083

Closed
Firepixel85 opened this issue Nov 3, 2024 · 4 comments
Closed

A way to unzip .zip files #11083

Firepixel85 opened this issue Nov 3, 2024 · 4 comments

Comments

@Firepixel85
Copy link

Firepixel85 commented Nov 3, 2024

Describe the project you are working on

An applications installer.

Describe the problem or limitation you are having in your project

The installer needs to download a .zip file from a remote server and then extract it's contents, but there seems to be no way to extract a .zip file.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add a method to the FileAccess object that takes in the file path for a .zip file, unzips it and places all its contents in a new file with the same name as the .zip file

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

In GDscript:
FileAccess.unzip(filepath: String)

Example Usecase:

extends Node

func unzip_app():
    FileAcces.unzip("user//application.zip")
    run_app()

func run_app():
    OS.shell_open("user://application/ExampleApp.exe")

If this enhancement will not be used often, can it be worked around with a few lines of script?

As this is a recommendation for GDscript and not Godot this could not be fixed with a script, because GDscript offers no such functionality.

Is there a reason why this should be core and not an add-on in the asset library?

As stated above there is no way to do something like this with an add-on at least to my knowledge.

@AThousandShips
Copy link
Member

Is ZIPReader not sufficient? Do you need some specific feature it doesn't cover?

@Firepixel85
Copy link
Author

ZIPReader only reads the file. Sadly in my case I need to be able to actually unzip the file. By the way there is a way of doing this in Godot by using OS.execute and the unzip command in bash but i believe there should be an easier way to do this.

@raulsntos
Copy link
Member

You should be able to just write the files you read with ZIPReader, here's a quick proof of concept:

var reader := ZIPReader.new()
reader.open("user://application.zip")

# Destination directory.
var root_dir := DirAccess.open("user://")

var files := reader.get_files()
for file_path in files:
    # If the current entry is a directory.
    if file_path.ends_with("/"):
        root_dir.make_dir(file_path)
        continue

    # Write file contents.
    var buffer := reader.read_file(file_path)
    var file := FileAccess.open(root_dir.get_current_dir().path_join(file_path), FileAccess.WRITE)
    file.store_buffer(buffer)

@Firepixel85
Copy link
Author

Ok yeah my mistake for not testing the ZIPReader extensively, both my method and @raulsntos work, I'll be it the ladder is easier to pull of. As such I am closing this issue. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants