Skip to content

Instantly share code, notes, and snippets.

@Bilal2453
Created February 19, 2023 20:06
Show Gist options
  • Save Bilal2453/831cc25bc32732febe0051652c54b5e4 to your computer and use it in GitHub Desktop.
Save Bilal2453/831cc25bc32732febe0051652c54b5e4 to your computer and use it in GitHub Desktop.
-- A simple showcase of how to possibly use miniz, this defines a function
-- extract, which takes a string to the path of a zip file
-- and extracts all of its contents into a folder in the same path of the same name of the file
local miniz = require('miniz')
local path = require('path')
local fs = require('fs')
local function extract(zip_file)
local reader = miniz.new_reader(zip_file)
if not reader then
error('Path is not a zip file')
end
local dirname = path.basename(zip_file, '.zip')
-- we don't use the same name as it is not allowed to have a folder and a file of the same name on some filesystems
assert(fs.mkdirSync(dirname .. '_extracted'))
local total_files = reader:get_num_files()
for i = 1, total_files do
local fpath = path.join(dirname, reader:get_filename(i))
assert(fs.writeFileSync(fpath, reader:extract(i)))
end
return total_files
end
extract('./test.zip')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment