From 58b7dfe85b124eaccc2d2f1018d9e4bc881acbbd Mon Sep 17 00:00:00 2001 From: Hans Hagen Date: Thu, 5 Dec 2019 19:41:53 +0100 Subject: 2019-12-05 18:54:00 --- scripts/context/lua/mtx-unzip.lua | 132 ++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 69 deletions(-) (limited to 'scripts/context/lua/mtx-unzip.lua') diff --git a/scripts/context/lua/mtx-unzip.lua b/scripts/context/lua/mtx-unzip.lua index 02d9676bc..0bc219386 100644 --- a/scripts/context/lua/mtx-unzip.lua +++ b/scripts/context/lua/mtx-unzip.lua @@ -8,7 +8,7 @@ if not modules then modules = { } end modules ['mtx-unzip'] = { -- maybe --pattern -local format = string.format +local format, find = string.format, string.find local helpinfo = [[ @@ -22,8 +22,7 @@ local helpinfo = [[ list files in archive - flatten unzipped directory structure - extract files + extract files [--silent --steps] @@ -41,92 +40,87 @@ local report = application.report scripts = scripts or { } scripts.unzipper = scripts.unzipper or { } -function scripts.unzipper.opened() +local function validfile() local filename = environment.files[1] if filename and filename ~= "" then filename = file.addsuffix(filename,'zip') - local zipfile = zip.open(filename) - if zipfile then - return zipfile + if lfs.isfile(filename) then + return filename + else + report("invalid zip file: %s",filename) end + else + report("no zip file") end - report("no zip file: %s",filename) return false end function scripts.unzipper.list() - local zipfile = scripts.unzipper.opened() - if zipfile then - local n = 0 - for k in zipfile:files() do - if #k.filename > n then n = #k.filename end - end - local files, paths, compressed, uncompressed = 0, 0, 0, 0 - local template_a = "%-"..n.."s" - local template_b = "%-"..n.."s % 9i % 9i" - local template_c = "\n%-"..n.."s % 9i % 9i" - for k in zipfile:files() do - if k.filename:find("/$") then - paths = paths + 1 - print(format(template_a, k.filename)) - else - files = files + 1 - local cs, us = k.compressed_size, k.uncompressed_size - if cs > compressed then - compressed = cs - end - if us > uncompressed then - uncompressed = us + local filename = validfile() + if filename then + local zipfile = utilities.zipfiles.open(filename) + if zipfile then + local list = utilities.zipfiles.list(zipfile) + if list then + local n = 0 + for i=1,#list do + local l = list[i] + if #l.filename > n then + n = #l.filename + end end - print(format(template_b,k.filename,cs,us)) + local files, paths, compressed, uncompressed = 0, 0, 0, 0 + local template_a = "%-" .. n .."s" + local template_b = "%-" .. n .."s % 9i % 9i" + local template_c = "\n%-" .. n .."s % 9i % 9i" + for i=1,#list do + local l = list[i] + local f = l.filename + if find(f,"/$") then + paths = paths + 1 + print(format(template_a, f)) + else + files = files + 1 + local cs = l.compressed + local us = l.uncompressed + if cs > compressed then + compressed = cs + end + if us > uncompressed then + uncompressed = us + end + print(format(template_b,f,cs,us)) + end + end -- check following pattern, n is not enough + print(format(template_c,files .. " files, " .. paths .. " directories",compressed,uncompressed)) end - end -- check following pattern, n is not enough - print(format(template_c,files .. " files, " .. paths .. " directories",compressed,uncompressed)) - end -end - -function zip.loaddata(zipfile,filename) - local f = zipfile:open(filename) - if f then - local data = f:read("*a") - f:close() - return data + utilities.zipfiles.close(zipfile) + else + report("invalid zip file: %s",filename) + end end - return nil end function scripts.unzipper.extract() - local zipfile = scripts.unzipper.opened() - if zipfile then - local junk = environment.arguments["j"] or environment.arguments["junk"] - for k in zipfile:files() do - local filename = k.filename - if filename:find("/$") then - if not junk then - lfs.mkdir(filename) - end - else - local data = zip.loaddata(zipfile,filename) - if data then - if junk then - filename = file.basename(filename) - end - io.savedata(filename,data) - print(filename) - end - end - end + local filename = validfile() + if validfile then + -- todo --junk + local silent = environment.arguments["silent"] + local steps = environment.arguments["steps"] + utilities.zipfiles.unzipdir { + zipname = filename, + path = ".", + verbose = not silent and (steps and "steps" or true), + } end end -if environment.arguments["h"] or environment.arguments["help"] then - application.help() -elseif environment.arguments["l"] or environment.arguments["list"] then - scripts.unzipper.list(zipfile) +if environment.arguments["list"] then + scripts.unzipper.list() +elseif environment.arguments["extract"] then + scripts.unzipper.extract() elseif environment.arguments["exporthelp"] then application.export(environment.arguments["exporthelp"],environment.files[1]) -elseif environment.files[1] then -- implicit --extract - scripts.unzipper.extract(zipfile) else application.help() end -- cgit v1.2.3