Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tEncode = {
- string = "a",
- table = "b",
- ["local"] = "c",
- ["function"] = "d",
- textutils = "e",
- paintutils = "f",
- ["while"] = "g",
- ["repeat"] = "h",
- ["until"] = "i",
- print = "j",
- ["return"] = "k",
- shell = "l",
- ["else"] = "m",
- ["end"] = "n",
- colour = "o",
- color = "o",
- setCursorPos = "p",
- setTextColour = "q",
- setTextColor = "q",
- setBackgroundColour = "r",
- setBackgroundColor = "r",
- number = "s",
- sleep = "t",
- rednet = "u",
- ["true"] = "v",
- ["false"] = "w",
- pairs = "x",
- math = "y",
- term = "z",
- pullEvent = "-"
- }
- local tDecode = {}
- for k, v in pairs(tEncode) do
- tDecode[v] = k
- end
- tArgs = {...}
- local function printUsage()
- local pr = shell.getRunningProgram()
- print("Usage: "..pr.." flags path ...\nFlags:\n c Create archive\n x Extract archive\n v Verbose\n f For creating archive specify output filename\n (default: archive.ar)\nExample:\n"..pr.." cvf sample.ar main.lua libs\n"..pr.." xv sample.ar")
- end
- local function splitName(sPath)
- local sPath = fs.getName(sPath)
- if not sPath:sub(2):find("%.") then return sPath, "" end
- local nDelim = sPath:find("%.")
- return sPath:sub(1, nDelim-1), sPath:sub(nDelim+1)
- end
- local function trySave(sPath, sData)
- if fs.exists(sPath) then
- local sF, sE = splitName(sPath)
- local sNewPath = sF.."-copy."..sE
- print(sPath..": File already exists.\n1) Skip\n2) Overwrite\n3) Save as "..sNewPath)
- while true do
- write("> ")
- local nOption = tonumber(read())
- if nOption==1 then
- return false
- elseif nOption==2 then
- local fHandle = fs.open(sPath, "w")
- fHandle.write(sData)
- fHandle.close()
- return true
- elseif nOption==3 then
- return trySave(sNewPath, sData)
- end
- end
- else
- local fHandle = fs.open(sPath, "w")
- fHandle.write(sData)
- fHandle.close()
- return true
- end
- end
- local function parse(sFile)
- local sPath = shell.resolve(sFile)
- if fs.isDir(sPath) then
- local sOut1, sOut2 = "", ""
- local tFiles = fs.list(sPath)
- print(tFiles)
- for i=1, #tFiles do
- local s1, s2 = parse(sFile.."/"..tFiles[i])
- sOut1 = sOut1..s1
- sOut2 = sOut2..s2
- end
- return sOut1, sOut2
- else
- local fHandle = fs.open(sPath, "r")
- local sContent = fHandle.readAll()
- fHandle.close()
- sContent = sContent:gsub("\\", "\\|")
- for k, v in pairs(tEncode) do
- sContent = sContent:gsub(k, "\\"..v)
- end
- return sFile.."\\/", sContent.."\\/"
- end
- end
- if #tArgs<2 then
- printUsage()
- return
- end
- local sFlags = tArgs[1]
- local tFiles = {}
- for i=2, #tArgs do tFiles[i-1] = tArgs[i] end
- local bEncoding = sFlags:find("c")~=nil
- local bDecoding = sFlags:find("x")~=nil
- local bVerbose = sFlags:find("v")~=nil
- local bOutputFile = sFlags:find("f")~=nil
- local sOutputFile = "archive.ar"
- if bOutputFile and bEncoding then
- if #tFiles<2 then
- printUsage()
- return
- end
- sOutputFile = shell.resolve(tFiles[1])
- table.remove(tFiles, 1)
- end
- if bEncoding==bDecoding then
- printUsage()
- return
- end
- if bDecoding then
- for i=1, #tFiles do
- local sFile = shell.resolve(tFiles[i])
- if not fs.exists(sFile) then
- print(sFile..": No such file or directory")
- elseif fs.isDir(sFile) then
- print(sFile..": Is a directory")
- else
- local fHandle = fs.open(sFile, "r")
- local sData = fHandle.readAll()
- fHandle.close()
- if sData:sub(1,2)~="AR" then
- print(sFile..": Is not an archive")
- else
- sData = sData:sub(3)
- local tFilenames = {}
- repeat
- local nDelim = sData:find("\\[\\/]")
- local sDelim = sData:sub(nDelim,nDelim+1)
- table.insert(tFilenames, shell.resolve(sData:sub(1, nDelim-1)))
- sData = sData:sub(nDelim+2)
- until sDelim=="\\\\"
- for i=1, #tFilenames do
- local nDelim = sData:find("\\\/")
- if nDelim==nil then
- print(sFile..": Malformed data")
- return
- end
- local sContent = sData:sub(1, nDelim-1)
- for k, v in pairs(tDecode) do
- sContent = sContent:gsub("\\"..k, v)
- end
- sContent = sContent:gsub("\\|", "\\")
- trySave(tFilenames[i], sContent)
- sData = sData:sub(nDelim+2)
- end
- end
- end
- end
- elseif bEncoding then
- local sOutput1 = "AR"
- local sOutput2 = ""
- for i=1, #tFiles do
- local sFile = shell.resolve(tFiles[i])
- if not fs.exists(sFile) then
- print(sFile..": No such file or directory")
- else
- local s1, s2 = parse(tFiles[i])
- sOutput1 = sOutput1..s1
- sOutput2 = sOutput2..s2
- end
- end
- sOutput1 = sOutput1:sub(1, #sOutput1-2)
- local sOutput = sOutput1.."\\\\"..sOutput2
- trySave(sOutputFile, sOutput)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement