Advertisement
Greygraphics

Lua file unpacker

May 12th, 2016
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.11 KB | None | 0 0
  1. --Set up everything
  2. local args = {...}
  3. local data = {}
  4. local files = 0
  5. local cursor = 0
  6.  
  7. --Open file and read contens into array
  8. local file = fs.open(args[1],"r")
  9. local ln = ""
  10.  
  11. while true do
  12.     ln = file.readLine()
  13.     if ln then
  14.         data[#data+1] = ln
  15.     else
  16.         break
  17.     end
  18. end
  19.  
  20. file.close()
  21. file = nil
  22. ln = nil
  23.  
  24. --Get amount of files stored
  25. for i=1,#data,1 do
  26.     if data[i] == "NEWFILE" then
  27.         files = files+1
  28.     end
  29. end
  30. print("Files found: "..files)
  31.  
  32. --Dump function
  33. local function dmp(s,c)
  34.     local newfile = fs.open(s,"w")
  35.     for i=1,#c,1 do
  36.         newfile.writeLine(c[i])
  37.     end
  38.     newfile.close()
  39.     print("Created file \""..s.."\"")
  40.     return
  41. end
  42.  
  43. --Goto file function
  44. local function nextfile()
  45.     repeat
  46.     cursor = cursor +1
  47.     until data[cursor] == "NEWFILE"
  48. end
  49.  
  50. --Read specific file
  51. for i=1,files,1 do
  52.     nextfile() --Goto next file
  53.     local cfile = {}
  54.     cursor = cursor +1
  55.     local name = data[cursor]
  56.     cursor = cursor +1
  57.     repeat
  58.         cfile[#cfile+1] = data[cursor]
  59.         cursor = cursor +1
  60.     until data[cursor] == "EOF"
  61.     dmp(name,cfile)
  62. end
  63.  
  64. --[[
  65. NEWFILE
  66. Hello
  67. print("Hello, world!")
  68. --Hello
  69. EOF
  70. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement