Created
March 11, 2026 10:32
-
-
Save 0riginaln0/d2037d469e3e36c87c1dea73bc8ffad1 to your computer and use it in GitHub Desktop.
Pack the Lua files in the current folder into an archive and unpack the archive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function unzip(input_path) | |
| -- Open the archive file for reading | |
| local archive_file = io.open(input_path, "r") | |
| if not archive_file then | |
| error("Could not open file: " .. input_path) | |
| end | |
| local current_file = nil | |
| local current_content = {} | |
| for line in archive_file:lines() do | |
| -- Check for a new file section | |
| if line:match("^--File:") then | |
| -- Save the previous file if it exists | |
| if current_file and #current_content > 0 then | |
| local file_output = io.open(current_file, "w") | |
| if file_output then | |
| file_output:write(table.concat(current_content, "\n")) | |
| file_output:close() | |
| else | |
| error("Could not create file: " .. current_file) | |
| end | |
| current_content = {} -- Reset content for the next file | |
| end | |
| -- Extract the file name | |
| current_file = line:match("^--File: (.+)") | |
| else | |
| -- Accumulate content for the current file being processed | |
| if current_file then | |
| table.insert(current_content, line) | |
| end | |
| end | |
| end | |
| -- Save the last file if it exists | |
| if current_file and #current_content > 0 then | |
| local file_output = io.open(current_file, "w") | |
| if file_output then | |
| file_output:write(table.concat(current_content, "\n")) | |
| file_output:close() | |
| else | |
| error("Could not create file: " .. current_file) | |
| end | |
| end | |
| archive_file:close() | |
| end | |
| local function zip() | |
| local function get_content(filename) | |
| local file = io.open(filename, "r") | |
| local content = file:read("*all") | |
| file:close() | |
| return content | |
| end | |
| local files = {} | |
| for filename in io.popen('ls *.lua'):lines() do | |
| if filename == "packer.lua" then | |
| goto continue | |
| end | |
| local commentfile = | |
| "\n--File: " .. filename .. "\n" .. | |
| get_content(filename) .. "\n" .. | |
| "--File: " .. filename | |
| files[filename] = commentfile | |
| ::continue:: | |
| end | |
| local archive = io.open("archive.lua", "w") | |
| if not archive then error("Could not create file: " .. filename) end | |
| for _, commentfile in pairs(files) do | |
| archive:write(commentfile) | |
| end | |
| archive:close() | |
| end | |
| -- Check for the input path as a command-line argument | |
| local usage = | |
| [[Usage: | |
| lua packer.lua zip | |
| lua packer.lua unzip <path_to_archive_file>]] | |
| if not arg then | |
| error(usage) | |
| end | |
| local mode = arg[1] | |
| local input_path = arg[2] | |
| if mode == "zip" then | |
| zip() | |
| elseif mode == "unzip" and input_path then | |
| unzip(input_path) | |
| else | |
| error(usage) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment