• Godot Help
  • Can a Godot app running on web create a zip file for the user to download?

I need to write an app that creates a zip file which the user can then download to their machine. I need two things - a library that can create a ZIP file and a way to have the user download that file, neither of which I can find. Can Godot do this?

  • kitfox replied to this.
  • kitfox Looks like Godot can export files for download using JavaScriptBridge and Godot 4 has provided a new ZIPPacker that lets you create zip archives.

    Here's some code packing some files into an archive and then downloading them through the web browser:

    	var writer:ZIPPacker = ZIPPacker.new()
    	var filepath:String = "user://archive.zip"
    	var err := writer.open(filepath)
    	if err != OK:
    		return err
    		
    	writer.start_file("hello.txt")
    	writer.write_file("Hello World".to_utf8_buffer())
    	writer.close_file()
    	writer.start_file("goodbye.txt")
    	writer.write_file("So long!".to_utf8_buffer())
    	writer.close_file()
    	writer.close()
    	
    	var f = FileAccess.open(filepath, FileAccess.READ)
    	var buf = f.get_buffer(f.get_length())
    	JavaScriptBridge.download_buffer(buf, "archive.zip", "application/zip")

    Wouldn't it be the server that creates and then serves that file? I'd think that's the typical way to go about it.

      Megalomaniak I know Javascript has the ability to do this entirely client side. I was hoping to do everything client side just to keep things simple.

        Yes, just look for Javascript libraries to do it.

        kitfox I know Javascript has the ability to do this entirely client side.

        If it's entirely dependent on data on the clientside/in browser then yes, I recon js is the way to go. And I think that the godot web player and js can talk to each other, though it's been a while since I've looked at that.

        kitfox Looks like Godot can export files for download using JavaScriptBridge and Godot 4 has provided a new ZIPPacker that lets you create zip archives.

        Here's some code packing some files into an archive and then downloading them through the web browser:

        	var writer:ZIPPacker = ZIPPacker.new()
        	var filepath:String = "user://archive.zip"
        	var err := writer.open(filepath)
        	if err != OK:
        		return err
        		
        	writer.start_file("hello.txt")
        	writer.write_file("Hello World".to_utf8_buffer())
        	writer.close_file()
        	writer.start_file("goodbye.txt")
        	writer.write_file("So long!".to_utf8_buffer())
        	writer.close_file()
        	writer.close()
        	
        	var f = FileAccess.open(filepath, FileAccess.READ)
        	var buf = f.get_buffer(f.get_length())
        	JavaScriptBridge.download_buffer(buf, "archive.zip", "application/zip")