OC-misc/lzsip/OpenOS/usr/bin/lzsip.lua

90 lines
1.7 KiB
Lua
Raw Normal View History

2020-02-18 17:45:07 +11:00
local lz = require "lzss"
local shell = pcall(require,shell)
local tA, tO = {}, {}
if shell then
tA, tO = shell.parse(...)
else
tA, tO = {...}, {}
end
tO.bsize = tO.bsize or 16384
local function cint(n,l)
local t={}
for i = 0, 7 do
t[i+1] = (n >> (i * 8)) & 0xFF
end
return string.reverse(string.char(table.unpack(t)):sub(1,l))
end
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 function cfile(fn)
local fi = io.open(fn,"rb")
if not fi then return false end
local fo = io.open(fn..".lss","wb")
if not fo then fi:close() return false end
fo:write("lz16") -- write header
while true do
local rblock = fi:read(tO.bsize)
if rblock == "" or not rblock then break end
local cblock = lz.compress(rblock)
fo:write(cint(cblock:len(),2)..cblock)
end
fi:close()
fo:close()
return true
end
local function xfile(fn)
local fi = io.open(fn,"rb")
if not fi then return false, "unable to open input file" end
if fi:read(4) ~= "lz16" and not tO.f then
return false, "not an lz16 archive"
end
local fo = io.open(fn:sub(1,-5),"wb")
if not fo then fi:close() return false, "unable to open output file" end
while true do
local len = toint(fi:read(2) or "\0\0")
if len < 1 then
break
end
fo:write(lz.decompress(fi:read(len)))
end
fi:close()
fo:close()
return true
end
if tO.x then -- extract
for k,v in pairs(tA) do
io.write(v.."...")
local s,r = xfile(v)
if s then
print(" done")
else
print(" failed: "..r)
end
end
else -- create
for k,v in pairs(tA) do
io.write(v.."...")
if cfile(v) then
print(" done.")
else
print(" failed.")
end
end
end