added mtar.iter to libmtar

This commit is contained in:
Izaya 2020-05-29 10:54:00 +10:00
parent b13fae7d29
commit f20719f3cb

View File

@ -54,6 +54,26 @@ function mtar.genHeader(fname,len) -- generate a header for file *fname* when pr
return string.format("%s%s%s",cint(fname:len(),2),fname,cint(len,2)) return string.format("%s%s%s",cint(fname:len(),2),fname,cint(len,2))
end end
function mtar.iter(stream) -- table -- function -- Given buffer *stream*, returns an iterator suitable for use with *for* that returns, for each iteration, the file name, a function to read from the file, and the length of the file.
local remain = 0
local function read(n)
local rb = stream:read(math.min(n,remain))
remain = remain - rb:len()
return rb
end
return function()
stream:read(remain)
local nlen = toint(stream:read(2) or "\0\0")
if nlen == 0 then
return
end
local name = cleanPath(stream:read(nlen))
local fsize = toint(stream:read(2))
remain = fsize
return name, read, fsize
end
end
function mtar.unarchive(stream,dest,verbose) -- Extract mtar archive read from *stream* to *dest*. If *verbose*, print status. function mtar.unarchive(stream,dest,verbose) -- Extract mtar archive read from *stream* to *dest*. If *verbose*, print status.
dest = dest or "." dest = dest or "."
while true do while true do