From 456136ef710be36c96d21abb14c8b120702a3035 Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Tue, 18 Sep 2012 23:40:18 +0100 Subject: [PATCH] Fix the calculation of the total unpacked size of the content of a zip file when it contains multiple entries. Also, don't write to temporary files in the physical filesystem --- use pipes instead. --- fileinfo.lua | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/fileinfo.lua b/fileinfo.lua index c04dfa825..c75e26bcd 100644 --- a/fileinfo.lua +++ b/fileinfo.lua @@ -30,6 +30,15 @@ function FileInfo:FileSize(size) end end +function getUnpackedZipSize(zipfile) + local cmd='unzip -l '..zipfile..' | tail -1 | sed -e "s/^ *\\([0-9][0-9]*\\) *.*/\\1/"' + local p = assert(io.popen(cmd, "r")) + local res = assert(p:read("*a")) + p:close() + res = string.gsub(res, "[\n\r]+", "") + return tonumber(res) +end + function FileInfo:init(path, fname) self.pathfile = path.."/"..fname self.result = {} @@ -45,25 +54,16 @@ function FileInfo:init(path, fname) info_entry = {dir = "Size", name = FileInfo:FileSize(lfs.attributes(self.pathfile, "size"))} table.insert(self.result, info_entry) - -- size & filename of unzipped entry for zips - if string.lower(string.match(fname, ".+%.([^.]+)")) == "zip" then - local outfile = "./data/zip_content" - local l, s = 1 - os.execute("unzip -l \""..self.pathfile.."\" > "..outfile) - if io.open(outfile, "r") then - for lines in io.lines(outfile) do - if l == 4 then s = lines break else l = l + 1 end - end - if s then - info_entry = { dir = "Unpacked", name = FileInfo:FileSize(tonumber(string.sub(s,1,11))) } - table.insert(self.result, info_entry) - end - --[[ TODO: When the fileentry inside zips is encoded as ANSI (codes 128-255) - any attempt to print such fileentry causes crash by drawing!!! When fileentries - are encoded as UTF8, everything seems fine - info_entry = { dir = "Content", name = string.sub(s,29,-1) } - table.insert(self.result, info_entry) ]] - end + -- total size of all unzipped entries for zips + local match = string.match(fname, ".+%.([^.]+)") + if match and string.lower(match) == "zip" then + info_entry = {dir = "Unpacked", name = FileInfo:FileSize(getUnpackedZipSize(self.pathfile))} + table.insert(self.result, info_entry) + --[[ TODO: When the fileentry inside zips is encoded as ANSI (codes 128-255) + any attempt to print such fileentry causes crash by drawing!!! When fileentries + are encoded as UTF8, everything seems fine + info_entry = { dir = "Content", name = string.sub(s,29,-1) } + table.insert(self.result, info_entry) ]] end info_entry = {dir = "Created", name = FileInfo:FileCreated(self.pathfile, "change")}