I have looked at the
ExtractZip snippet again and below is my proposed script.
It adds the
gsub("\\$","") to strip optional trailing backslashes from paths.
It has only one iteration loop
for file in zfile:files() do that both creates new paths and copies files.
Was there a reason why your original needed to have two loops?
Code: Select all
function ExtractZip(zipPath, zipFilename, destinationPath)
zipPath = zipPath:gsub("\\$","")
destinationPath = destinationPath:gsub("\\$","")
local zfile = assert(zip.open(zipPath.."\\"..zipFilename),"Failed to open zip file "..zipPath.."\\"..zipFilename) --open the zip file for processing
local function CopyFile(file)
local currFile, err = zfile:open(file.filename) --open a file within the zipfile
local currFileContents = currFile:read("*a") -- read entire contents of current file
local hBinaryOutput = io.open(destinationPath .."\\".. file.filename, "wb") --open an outputfile
-- write current file inside zip to a file outside zip
if hBinaryOutput then
hBinaryOutput:write(currFileContents) --write the new file as a copy of the file within the zipfile
hBinaryOutput:close() --close the new file
end
currFile:close() --close the file within the zipfile
end
-- iterate through each file inside the zip file
for file in zfile:files() do
local newdir = path.dirname(file.filename)
if not path.exists(destinationPath.."\\"..newdir) then dir.makepath(destinationPath.."\\"..newdir) end
if path.basename(file.filename) ~= "" then CopyFile(file) end
end
zfile:close()
end