Advertisement
Redxone

[ComputerCraft] OneFile - File Archiver

Jun 2nd, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. function file_readAll(file,s)
  2.     local f = fs.open(file,"r")
  3.     local cont = f.readAll()
  4.     f.close()
  5.     if(s)then return textutils.unserialize(s) end
  6.     return cont
  7. end
  8. function file_readLine(file,s)
  9.     local f = fs.open(file,"r")
  10.     local cont = f.readLine()
  11.     f.close()
  12.     if(s)then return textutils.unserialize(s) end
  13.     return cont
  14. end
  15. function file_write(file,data,s)
  16.     local f = fs.open(file,"w")
  17.     if(s)then f.write(textutils.serialize(data)) else
  18.     f.write(data) end
  19.     f.close()
  20. end
  21. function file_writeline(file,data,s)
  22.     local f = fs.open(file,"w")
  23.     if(s)then f.writeLine(textutils.serialize(data)) else
  24.     f.writeLine(data) end
  25.     f.close()
  26. end
  27. local function formatpath(path)
  28.     local pathtbl={}
  29.     local s=""
  30.     local pathsize=0
  31.     -- Divide string and count the number of
  32.     -- divisions.
  33.      for str in string.gmatch(path, "([^/]+)") do
  34.          pathtbl[#pathtbl+1] = str  
  35.      end
  36.      table.remove(pathtbl,1)
  37.      for i = 1, #pathtbl do
  38.         s = s .. pathtbl[i] .. "/"
  39.      end
  40.     return s
  41. end
  42.  
  43. local function getcontent(data,wdata,ctbl)
  44.     -- Returns a table --
  45.     for k, v in pairs(fs.list(data)) do
  46.      
  47.       if(fs.isDir(data.."/"..v) )then
  48.         getcontent(data..v.."/",v.."/",ctbl)
  49.       else
  50.       --print(data..v)
  51.         local f = fs.open(data..v,"r")
  52.         ctbl[formatpath(data..v)] = {content=f.readAll()}
  53.         f.close()
  54.       end
  55.     end
  56.      return textutils.serialize(ctbl)
  57. end
  58. function archive(data,export)
  59.     local content = {}
  60.     content = getcontent('/' .. data .. "/","/",content)
  61.     file_write(export,content)
  62.     return true
  63. end
  64.  
  65. function extract(file,to)
  66.     local cont = textutils.unserialize(file_readAll(file))
  67.     for k,v in pairs(cont) do
  68.          file_write(to .. "/" .. k,v.content)
  69.     end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement