From eaef339fe93b54795aa07af7070fa406e5cc4758 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Mon, 22 Jul 2019 13:47:21 +1000 Subject: [PATCH] added an unarchiving program, changed mkarchive behavior slightly --- exec/mkarchive.lua | 4 ++-- exec/unarchive.lua | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 exec/unarchive.lua diff --git a/exec/mkarchive.lua b/exec/mkarchive.lua index bad1a2e..ce860a5 100644 --- a/exec/mkarchive.lua +++ b/exec/mkarchive.lua @@ -1,7 +1,7 @@ local tArgs = {...} -local output = tArgs[2] +local output = table.remove(tArgs,#tArgs) local of = io.open(output,"wb") -local files, dirs = {}, {tArgs[1]} +local files, dirs = {}, {tArgs[1] or "."} local function cint(n,l) local t={} diff --git a/exec/unarchive.lua b/exec/unarchive.lua new file mode 100644 index 0000000..13d4296 --- /dev/null +++ b/exec/unarchive.lua @@ -0,0 +1,51 @@ +local tArgs = {...} + +local function toint(s) + s=s or "" + local n = 0 + local i = 1 + while true do + local p = s:sub(i,i) + if p == "" then break end + local b = string.byte(p) + n = n << 8 + n = n | b + i=i+1 + end + return n +end + +local fi = io.open(tArgs[1]) +while true do + local nlen = toint(fi:read(2)) + if nlen == 0 then + break + end + local name = fi:read(nlen) + local fsize = toint(fi:read(2)) + io.write(string.format("%s: %d... ",name,fsize)) + if not tArgs[2] then + local dir = name:match("(.+)/.*%.?.+") + if (dir) then + fs.makeDirectory(dir) + end + local f = io.open(name,"wb") + local rsize,buf = fsize, "" + if f then + repeat + buf = fi:read(math.min(rsize,1024)) + f:write(buf) + rsize = rsize - buf:len() + until rsize <= 1 + f:close() + end + else + local rsize = fsize + repeat + buf = fi:read(math.min(rsize,1024)) + rsize = rsize - buf:len() + until rsize <= 1 + end + print(fsize) +end +fi:close()