Advertisement
Guest User

arch

a guest
Jun 26th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. local tEncode = {
  2.   string = "a",
  3.   table = "b",
  4.   ["local"] = "c",
  5.   ["function"] = "d",
  6.   textutils = "e",
  7.   paintutils = "f",
  8.   ["while"] = "g",
  9.   ["repeat"] = "h",
  10.   ["until"] = "i",
  11.   print = "j",
  12.   ["return"] = "k",
  13.   shell = "l",
  14.   ["else"] = "m",
  15.   ["end"] = "n",
  16.   colour = "o",
  17.   color = "o",
  18.   setCursorPos = "p",
  19.   setTextColour = "q",
  20.   setTextColor = "q",
  21.   setBackgroundColour = "r",
  22.   setBackgroundColor = "r",
  23.   number = "s",
  24.   sleep = "t",
  25.   rednet = "u",
  26.   ["true"] = "v",
  27.   ["false"] = "w",
  28.   pairs = "x",
  29.   math = "y",
  30.   term = "z",
  31.   pullEvent = "-"
  32. }
  33.  
  34. local tDecode = {}
  35. for k, v in pairs(tEncode) do
  36.   tDecode[v] = k
  37. end
  38.  
  39. tArgs = {...}
  40.  
  41. local function printUsage()
  42.   local pr = shell.getRunningProgram()
  43.   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")
  44. end
  45.  
  46. local function splitName(sPath)
  47.   local sPath = fs.getName(sPath)
  48.   if not sPath:sub(2):find("%.") then return sPath, "" end
  49.   local nDelim = sPath:find("%.")
  50.   return sPath:sub(1, nDelim-1), sPath:sub(nDelim+1)
  51. end
  52.  
  53. local function trySave(sPath, sData)
  54.   if fs.exists(sPath) then
  55.     local sF, sE = splitName(sPath)
  56.     local sNewPath = sF.."-copy."..sE
  57.     print(sPath..": File already exists.\n1) Skip\n2) Overwrite\n3) Save as "..sNewPath)
  58.     while true do
  59.       write("> ")
  60.       local nOption = tonumber(read())
  61.       if nOption==1 then
  62.         return false
  63.       elseif nOption==2 then
  64.         local fHandle = fs.open(sPath, "w")
  65.         fHandle.write(sData)
  66.         fHandle.close()
  67.         return true
  68.       elseif nOption==3 then
  69.         return trySave(sNewPath, sData)
  70.       end
  71.     end
  72.   else
  73.     local fHandle = fs.open(sPath, "w")
  74.     fHandle.write(sData)
  75.     fHandle.close()
  76.     return true
  77.   end
  78. end
  79.  
  80. local function parse(sFile)
  81.   local sPath = shell.resolve(sFile)
  82.   if fs.isDir(sPath) then
  83.     local sOut1, sOut2 = "", ""
  84.  
  85.     local tFiles = fs.list(sPath)
  86.     print(tFiles)
  87.     for i=1, #tFiles do
  88.       local s1, s2 = parse(sFile.."/"..tFiles[i])
  89.       sOut1 = sOut1..s1
  90.       sOut2 = sOut2..s2
  91.     end
  92.  
  93.     return sOut1, sOut2
  94.   else
  95.     local fHandle = fs.open(sPath, "r")
  96.     local sContent = fHandle.readAll()
  97.     fHandle.close()
  98.     sContent = sContent:gsub("\\", "\\|")
  99.     for k, v in pairs(tEncode) do
  100.       sContent = sContent:gsub(k, "\\"..v)
  101.     end
  102.     return sFile.."\\/", sContent.."\\/"
  103.   end
  104. end
  105.  
  106. if #tArgs<2 then
  107.   printUsage()
  108.   return
  109. end
  110.  
  111. local sFlags = tArgs[1]
  112. local tFiles = {}
  113. for i=2, #tArgs do tFiles[i-1] = tArgs[i] end
  114.  
  115. local bEncoding = sFlags:find("c")~=nil
  116. local bDecoding = sFlags:find("x")~=nil
  117. local bVerbose = sFlags:find("v")~=nil
  118. local bOutputFile = sFlags:find("f")~=nil
  119. local sOutputFile = "archive.ar"
  120.  
  121. if bOutputFile and bEncoding then
  122.   if #tFiles<2 then
  123.     printUsage()
  124.     return
  125.   end
  126.   sOutputFile = shell.resolve(tFiles[1])
  127.   table.remove(tFiles, 1)
  128. end
  129.  
  130. if bEncoding==bDecoding then
  131.   printUsage()
  132.   return
  133. end
  134.  
  135. if bDecoding then
  136.    
  137.   for i=1, #tFiles do
  138.     local sFile = shell.resolve(tFiles[i])
  139.     if not fs.exists(sFile) then
  140.       print(sFile..": No such file or directory")
  141.     elseif fs.isDir(sFile) then
  142.       print(sFile..": Is a directory")
  143.     else
  144.       local fHandle = fs.open(sFile, "r")
  145.       local sData = fHandle.readAll()
  146.       fHandle.close()
  147.       if sData:sub(1,2)~="AR" then
  148.         print(sFile..": Is not an archive")
  149.       else
  150.  
  151.         sData = sData:sub(3)
  152.         local tFilenames = {}
  153.         repeat
  154.           local nDelim = sData:find("\\[\\/]")
  155.           local sDelim = sData:sub(nDelim,nDelim+1)
  156.           table.insert(tFilenames, shell.resolve(sData:sub(1, nDelim-1)))
  157.           sData = sData:sub(nDelim+2)
  158.         until sDelim=="\\\\"
  159.  
  160.         for i=1, #tFilenames do
  161.           local nDelim = sData:find("\\\/")
  162.           if nDelim==nil then
  163.             print(sFile..": Malformed data")
  164.             return
  165.           end
  166.           local sContent = sData:sub(1, nDelim-1)
  167.           for k, v in pairs(tDecode) do
  168.             sContent = sContent:gsub("\\"..k, v)
  169.           end
  170.           sContent = sContent:gsub("\\|", "\\")
  171.           trySave(tFilenames[i], sContent)
  172.           sData = sData:sub(nDelim+2)
  173.         end
  174.  
  175.       end
  176.     end
  177.   end
  178.  
  179. elseif bEncoding then
  180.  
  181.   local sOutput1 = "AR"
  182.   local sOutput2 = ""
  183.  
  184.   for i=1, #tFiles do
  185.     local sFile = shell.resolve(tFiles[i])
  186.     if not fs.exists(sFile) then
  187.       print(sFile..": No such file or directory")
  188.     else
  189.       local s1, s2 = parse(tFiles[i])
  190.       sOutput1 = sOutput1..s1
  191.       sOutput2 = sOutput2..s2
  192.     end
  193.   end
  194.  
  195.   sOutput1 = sOutput1:sub(1, #sOutput1-2)
  196.  
  197.   local sOutput = sOutput1.."\\\\"..sOutput2
  198.   trySave(sOutputFile, sOutput)
  199.  
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement