Add compress command line tool

This commit is contained in:
Kirigaya Kazuto 2019-01-07 23:15:32 +08:00
parent 306783ca2c
commit 02e9a703b0
2 changed files with 41 additions and 1 deletions

39
bin/compress.lua Normal file
View File

@ -0,0 +1,39 @@
-- Compress command line tool
-- Powered by libcompress
local shell=require('shell')
local libcompress=require('libcompress')
local args,opts=shell.parse(...)
if(args<2) then
print("Usage:\n\tcompress [-dv] <source> <dest>")
end
local verbose=opts["v"] and print or function() end
local f=io.open(args[1],"rb")
if(not f) then
print("[Error] Failed to open file for read: " .. args[1])
return
end
local str=f:read("a")
f:close()
local nSource=str:len()
str=opts["d"] and libcompress.inflate(str) or libcompress.deflate(str)
local nDest=str:len()
verbose(string.format("SourceLen: %d DestLen: %d Rate: %.2f",nSource,nDest,nDest/nSource))
f=io.open(args[2],"wb")
if(not f) then
print("[Error] Failed to open file for write: " .. args[2])
return
end
local ok,msg=f:write(str)
f:close()
if(not ok) then
print("[Error] Failed to write file: " .. args[2] .. ": " .. msg)
return
end

View File

@ -79,7 +79,8 @@
title="Compression Library",
info="Inflate and deflate library for general purpose.",
files={
["libs/libcompress.lua"]="__lib__/"
["libs/libcompress.lua"]="__lib__/",
["bin/compress.lua"]="__bin__/"
},
requires={
"libkeepup"