Advertisement
fishermedders

Computercraft Archiver

Nov 4th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. function saveFile( file, data )
  4. local f = fs.open( file, "w" )
  5. f.write( data )
  6. f.close( )
  7. end
  8.  
  9. function readFile( file )
  10. if fs.exists( file ) then
  11. local f = fs.open( file, "r" )
  12. local inside = f.readAll( )
  13. f.close()
  14. return inside
  15. end
  16. end
  17.  
  18. function tableToFile( t, folder )
  19. if not fs.exists( folder ) then fs.makeDir( folder ) end
  20.  
  21. for k, v in pairs( t ) do
  22. if type( v ) == "table" then
  23. tableToFile( v, folder.."/"..k )
  24. end
  25. if type( v ) == "string" then
  26. saveFile( folder.."/"..k, v )
  27. end
  28. end
  29. end
  30.  
  31. function filelistToTable( folder )
  32. local toReturn = { }
  33. local t = fs.list( folder )
  34. for i = 1, #t do
  35. if #t[i] >= 4 then
  36. if t[i]:sub(1,4) ~= "rom/" then
  37. print(t[i])
  38. if fs.isDir( folder.."/"..t[i] ) then
  39. toReturn[t[i]] = filelistToTable( folder.."/"..t[i] )
  40. else
  41. toReturn[t[i]] = readFile( folder.."/"..t[i] )
  42. end
  43. sleep(0.05)
  44. end
  45. end
  46. end
  47. return toReturn
  48. end
  49.  
  50. function printUsage( )
  51. print "archive pack <folder> <archive>"
  52. print "archive compress <folder> <archive>"
  53. print "archive unpack <archive> <folder>"
  54. print "archive extract <archive> <folder>"
  55. print "archive installer <archive> <installer>"
  56. print "archive selfextract <archive> <installer>"
  57. end
  58.  
  59. if tArgs[1] == "pack" or tArgs[1] == "compress" then
  60. if #tArgs < 3 then printUsage( ) return end
  61. local Archive = { }
  62.  
  63. if fs.exists( tArgs[2] ) then
  64. print "done prepairing, making archive"
  65. Archive = filelistToTable( tArgs[2] )
  66. print "done making archive now saving"
  67. saveFile( tArgs[3], textutils.serialize( Archive ) )
  68. print "done saving archive"
  69. else
  70. print "folder does not exist"
  71. end
  72.  
  73. end
  74.  
  75. if tArgs[1] == "unpack" or tArgs[1] == "extract" then
  76. if #tArgs < 3 then printUsage( ) return end
  77.  
  78. print "unpacking/extracting..."
  79. tableToFile( textutils.unserialize( readFile( tArgs[2] ) ), tArgs[3] )
  80. print "unpacking/extracting complete"
  81.  
  82. end
  83.  
  84. if tArgs[1] == "installer" or tArgs[1] == "selfextract" then
  85. print( "how is should the archive/program be called?" )
  86. local name = read( )
  87.  
  88. print( "what is the folder to install extract it in? leave blank if you want to let the user enter one" )
  89. local instdir = read( )
  90.  
  91. print( "what is the message for installing it?" )
  92. local msg = read( )
  93.  
  94. print( "what os the message when its done?")
  95. local msgdone = read( )
  96.  
  97. print( "what program should it execute when its done? leave blank for none" )
  98. local doneexe = read( )
  99.  
  100. local s = [[
  101. local archive = ]]..readFile( tArgs[2] )..[[
  102. local installfolder = ']]..instdir..[['
  103. local name = ']]..name..[['
  104. local msg = ']]..msg..[['
  105. local msgdone = ']]..msgdone..[['
  106. local doneexe = ']]..doneexe..[['
  107.  
  108. function saveFile( file, data )
  109. local f = fs.open( file, 'w' )
  110. f.write( data )
  111. f.close( )
  112. end
  113.  
  114. function tableToFile( t, folder )
  115. if not fs.exists( folder ) then fs.makeDir( folder ) end
  116. for k, v in pairs( t ) do
  117. if type( v ) == 'table' then
  118. tableToFile( v, folder..'/'..k )
  119. end
  120. if type( v ) == 'string' then
  121. saveFile( folder..'/'..k, v )
  122. end
  123. end
  124. end
  125.  
  126. if installfolder == '' then
  127. print 'please enter the folder where the archive is going to be installed in'
  128. installfolder = read( )
  129. end
  130. print( msg )
  131. tableToFile( archive, installfolder )
  132. print( msgdone )
  133. if doneexe ~= "" then shell.run( doneexe ) end
  134. ]]
  135.  
  136. local handle = io.open( tArgs[3], "w" )
  137. handle:write( s )
  138. handle:close( )
  139. print "done making installer/self extractor"
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement