Miscellaneous OpenComputers stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.7 KiB

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