Advertisement
LDDestroier

Basic package maker

May 29th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1. function displayHelp()
  2.     print("Basic packager by Eldidi")
  3.     print(" red -d <source> <dest>")
  4.     print(" red -c <dest> <file1>, [file2]...")
  5. end
  6.  
  7. function compress(files, dest)
  8.     if type(files) ~= "table" then
  9.         error("table expected, somehow got " .. type(files))
  10.     end
  11.     if type(dest) ~= "string" then
  12.         error("string expected, somehow got " .. type(dest))
  13.     end
  14.     fileList = files
  15.     fileTable = {}
  16.     for a = 1, #fileList do
  17.         file = fs.open(fileList[a], "r")
  18.         contents = file.readAll()
  19.         table.insert(fileTable, {fileList[a], contents})
  20.         file.close()
  21.     end
  22.     file = fs.open(dest, "w")
  23.     file.write(fileTable)
  24.     file.close()
  25. end
  26.  
  27. function decompress(source, dest)
  28.     if type(source) ~= "string" then
  29.         error("string expected, got " .. type(source))
  30.     end
  31.     if type(dest) ~= "string" then
  32.         error("string expected, got " .. type(dest))
  33.     end
  34.     if not fs.exists(source) then
  35.         error(source .. " does not exist")
  36.     end
  37.     if fs.exists(dest) then
  38.         print("overwrite " .. dest .. "? (Y/N)")
  39.         ans = tostring(read())
  40.         if string.lower(ans) == "n" then
  41.             error("overwrite declined")
  42.         end
  43.     end
  44.     file = fs.open(source, "r")
  45.     contents = file.readAll()
  46.     file.close()
  47.     tabContents = textutils.unserialize(contents)
  48.     for a = 1, #tabContents do
  49.         fileContents = tabContents[a]
  50.         fileName = fileContents[1]
  51.         table.delete(1, fileContents)
  52.         file = fs.open(fileName, "w")
  53.         file.writeLine(textutils.serialize(fileContents))
  54.         file.close()
  55.     end
  56. end
  57.  
  58. tArg = {...}
  59. if #tArg < 2 then
  60.     displayHelp()
  61.     return
  62. end
  63. if tArg[1] == "-d" then
  64.     decompress(tArg[2], tArg[3])
  65.     return
  66. elseif tArg[1] == "-c" then
  67.     arguments = {}
  68.     for a = 3, #tArg do
  69.         table.insert(arguments, tArg[a])
  70.     end
  71.     compress(arguments, tArg[2])
  72.     return
  73. else
  74.     displayHelp()
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement