This repository has been archived on 2024-04-19. You can view files and clone it, but cannot push or open issues or pull requests.
FREAX/bin/archiver
2022-07-18 19:33:40 +02:00

131 lines
No EOL
2.8 KiB
Text

print("Debian CC Archiver v0.1 by 1Ridav")
tArgs = {...}
local function getPath(input)
if string.sub(input, 1, 1)=="/" then path=input else
path=("/" .. shell.dir() .. "/" .. input) end
return path
end
if tArgs[2] then
tArgs[2]=getPath(tArgs[2])
end
local FFormat = ".arch"
local nFile, nDir, size = 0, 0
if #tArgs < 3 then
print("Usage:"
.."\narchiver zip <DIR> <ZIPname> [DIRtoSkip] [DIRtoSkip]"
.."\narchiver unzip <ZIPname> <DIR>")
end
local function fopen(path, mode)
local f = fs.open(path, mode)
if not f then
print("ERROR: Could not open "..path.." with mode \""..mode.."\"")
exit()
end
return f
end
local function skip(df)
for i = 3, #tArgs do
if tArgs[i] == fs.getName(df) then
return true
end
end
for i = 4, #tArgs do
if tArgs[i] == fs.getName(df) then
return true
end
end
return false
end
local function zip(file)
print("zipping: ".. file)
local f = fopen(file, "r")
local z = textutils.serialize(f.readAll())
f.close()
return z
end
local function ZIP(path)
local list = fs.list(path)
local array = {}
local t, name, d = 0, "", 0
for i = 2, #list * 2, 2 do
t = i/2
local tpath = path.."/"..list[t]
if fs.isDir(tpath) then
if not skip(tpath) then
name = "D:"..list[t]
array[i] = ZIP(tpath)
nDir = nDir + 1
end
else
name = "F:"..list[t]
array[i] = zip(tpath)
nFile = nFile + 1
end
array[i - 1] = name
end
return textutils.serialize(array)
end
local function unzip(text, path)
print("unzipping: "..path)
local f = fopen(path, "w")
f.write(textutils.unserialize(text))
f.close()
end
local function UNZIP(text, path)
local array = textutils.unserialize(text)
local unz, dp
local d = 0
for i = 2, #array, 2 do
if string.sub(array[i-1], 1, 1) == "D" then
dp = string.sub(array[i-1], 3, #array[i-1])
fs.makeDir(path.."/"..dp)
UNZIP(array[i], path.."/"..dp)
nDir = nDir + 1
elseif string.sub(array[i-1], 1, 1) == "F" then
local p = string.sub(array[i-1], 3, #array[i-1])
unzip(array[i], path.."/"..p)
nFile = nFile + 1
end
end
end
local function result()
print("\nDone"
,"\n size: "
,size, " B "
,math.floor(size/1024), " KB"
,"\n Files: ", nFile
,"\n Folders: ", nDir
)
end
if tArgs[1] == "zip" then
if fs.exists(tArgs[2]) and fs.isDir(tArgs[2]) then
local zipped = ZIP(shell.resolve(tArgs[2]))
local f = fs.open(tArgs[3]..FFormat, "w")
f.write(zipped)
f.close()
zipped = nil
size = fs.getSize(tArgs[3]..FFormat)
result()
end
elseif tArgs[1] == "unzip" then
local f = fopen(tArgs[2], "r")
if not fs.exists(tArgs[3]) then
fs.makeDir(tArgs[3])
end
UNZIP(f.readAll(), tArgs[3])
size = fs.getSize(tArgs[2])
result()
end