Advertisement
Greygraphics

File extractor

May 11th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.47 KB | None | 0 0
  1. --[[
  2. This programm can extract many files from a single one, even itself.
  3. An example of this is given at the end of this code. Feel free to use it, but please give credit.
  4. --]]
  5.  
  6. --Set up everything
  7. local args = {...}
  8. local data = {}
  9. local files = 0
  10. local cursor = 1
  11.  
  12. --Open file and read contens into array
  13. local file = fs.open(args[1],"r")
  14. local ln = ""
  15.  
  16. while true do
  17.     ln = file.readLine()
  18.     if ln then
  19.         data[#data+1] = ln
  20.     else
  21.         break
  22.     end
  23. end
  24.  
  25. file.close()
  26. file = nil
  27. ln = nil
  28.  
  29. --Get amount of files stored
  30. for i=1,#data,1 do
  31.     if data[i] == "--[[NEWFILE" then
  32.         files = files+1
  33.     end
  34. end
  35. print("Files found: "..files)
  36.  
  37. --Dump function
  38. local function dmp(s,c)
  39.     local newfile = fs.open(s,"w")
  40.     for i=1,#c,1 do
  41.         newfile.writeLine(c[i])
  42.     end
  43.     newfile.close()
  44.     print("Created file \""..s.."\"")
  45.     return
  46. end
  47.  
  48. --Goto file function
  49. local function nextfile()
  50.     repeat
  51.     cursor = cursor +1
  52.     until data[cursor] == "--[[NEWFILE"
  53. end
  54.  
  55. --Read specific file
  56. for i=1,files,1 do
  57.     nextfile() --Goto next file
  58.     local cfile = {}
  59.     cursor = cursor +1
  60.     local name = data[cursor]
  61.     cursor = cursor +1
  62.     repeat
  63.         cfile[#cfile+1] = data[cursor]
  64.         cursor = cursor +1
  65.     until data[cursor] == "EOF--]]"
  66.     dmp(name,cfile)
  67. end
  68.  
  69. --This is an example
  70. --After the comment with NEWFILE, you write the name of the program, after that follows the code. You end with an EOF sign
  71. --This method allows a self-extracting file
  72.  
  73. --[[NEWFILE
  74. Hello
  75. print("Hello, world!")
  76. EOF--]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement