Unzipping files

Godot Version



How do I unzip a file (preferably in GDScript)? I am writing an application that deals with installing another application that is inside a ZIP archive. However, the default ZIPReader and ZIPPacker classes don’t have an extract function. I feel like writing a script that goes through every file in the ZIP and writing it can’t possibly be the best way to go about doing things. Google hasn’t gotten me anywhere- everything is made for Godot 3. I am willing to dabble in using C# but would prefer not to.

If it is relevant, the zip file is from Celeste64/Fuji.

tl;dr, how do I unzip a file in Godot?

I don’t really use GDScript, but as far as I can tell, ZIPReader can very much be used to “unzip” a zip file. It has functions for opening a zip file, getting the file list, and reading said files. With these, it’s not too difficult to create a loop that goes through all the files, and saves their contents somewhere else on the disk.


I ended up implementing that, and while not ideal it was OK. Here’s the code to do it if anyone wants it.

What this code does is just look at the ZIP archive and unzip all of its contents into a folder with the same name as the zip.

## Unzips stuff because godot doesn't have that already??
static func unzip(path_to_zip: String) -> void:
	var zr : ZIPReader = ZIPReader.new()
	if zr.open(path_to_zip) == OK:		
		for filepath in zr.get_files():
			var zip_directory : String = path_to_zip.get_base_dir()
			var da : DirAccess = DirAccess.open(zip_directory)
			var trimmed_path : String = path_to_zip.trim_suffix(".zip")
			da = DirAccess.open(trimmed_path)
			print(trimmed_path + filepath)
			var fa : FileAccess = FileAccess.open("%s/%s" % [trimmed_path, filepath], FileAccess.WRITE)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.