Advertisement
HPWebcamAble

[CC][2.0] Craft Compress

Mar 4th, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.16 KB | None | 0 0
  1. --[[
  2. HPWebcamAble Presents...
  3. Craft Compress
  4.  
  5. === Description ====
  6. Craft Compress makes directories into a single file
  7.  
  8. There are two versions
  9. One that also decompresses the files
  10. The other creates files that extract themselves
  11.  
  12. This is the second one
  13. (Pastebin code for the first: YbyvQDP6)
  14.  
  15. ==== Documentation ====
  16. ComputerCraft Forum Post:
  17. http://www.computercraft.info/forums2/index.php?/topic/22198-craft-compress/
  18.  
  19.  
  20. ==== Installation and Use ====
  21. Pastebin Code: JCCNWT8N
  22.  
  23. Run this in a computer to download from pastebin
  24.  
  25. pastebin get JCCNWT8N cc
  26.  
  27. Download this file onto the computer you want to use it on
  28. Type 'cc help' for usages
  29.  
  30.  
  31. ==== Update History ====
  32. The pastebin will always have the most recent version
  33.  
  34. |2.0| <-- This program
  35.   -Compressed files are extracted by running them (Like a program)
  36.  
  37. |1.0|
  38.   -Release
  39. ]]
  40.  
  41. args = {...}
  42. local file = {}
  43. local compressionVersion = 2
  44.  
  45. local function readFile(filePath)
  46.   local f = fs.open(filePath,"r")
  47.   local lineNum = 0
  48.   local fileLines = {}
  49.   repeat
  50.     lineNum = lineNum+1
  51.     fileLines[lineNum] = f.readLine()
  52.   until fileLines[lineNum] == nil
  53.   f.close()
  54.   return fileLines
  55. end
  56.  
  57. --This is the function that actually compresses the directroy
  58. --It needs to be a function because it calls itself
  59. local function compressPath(path,iPath)
  60.   path = shell.resolve(path)
  61.   local dir = fs.list(path)
  62.   for i = 1, #dir do
  63.     if fs.isDir(path.."/"..dir[i]) then
  64.       compressPath(path.."/"..dir[i],iPath.."/"..dir[i])
  65.     else
  66.       local tempFile = {}
  67.       table.insert(file,iPath.."/"..dir[i])
  68.       local tempFile = readFile(path.."/"..dir[i])
  69.       table.insert(file,#tempFile)
  70.       for i = 1, #tempFile do
  71.         table.insert(file,tempFile[i])
  72.       end
  73.     end
  74.   end
  75. end
  76.  
  77. local function main()
  78.   if #args == 0 then --No arguments
  79.     print("Craft Compress takes a directory and makes it into a single file")
  80.     print("Type 'cc help' for usages")
  81.     print("More info is in the file, type 'edit cc' to view")
  82.   elseif args[1] == "help" or not args[2] then --help
  83.     print("Usages:")
  84.     print("cc path destination")
  85.     print("Note: The file that is created uncompresses itself, just run it like a program")    
  86.   else --otherwise
  87.     args[1] = shell.resolve(args[1])
  88.     args[2] = shell.resolve(tostring(args[2]))
  89.     if not fs.isDir(args[1]) then --Check stuff is good
  90.       printError("The path must be a directory")
  91.       print("Usage:")
  92.       print("cc path destination")
  93.       return
  94.     elseif fs.exists(args[2]) then
  95.       printError("Destination exists")
  96.       print("Overwrite? y/n")
  97.       while true do
  98.         local event,key = os.pullEvent("key")
  99.         if key == keys.y then
  100.           print("Will overwrite...")
  101.           break
  102.         elseif key == keys.n then
  103.           printError("Canceled")
  104.           error()
  105.         end
  106.       end
  107.     end
  108.     f = fs.open(args[2],"w")
  109.     if not f then
  110.       printError("Unable to open destination path")
  111.       return
  112.     end
  113.     table.insert(file,"--[[")
  114.     table.insert(file,"This was created with Craft Compress by HPWebcamAble")
  115.     table.insert(file,"Run it to decompress")
  116.     print("Compressing "..args[1].." to "..args[2].."...")
  117.     compressPath(args[1],"") --Actually does the compressing
  118.     table.insert(file,"Below is the program that will decompress this")
  119.     table.insert(file,"]]")
  120.     for i = 1, #file do
  121.       f.write(file[i].."\n")
  122.     end
  123.     f.write([[
  124. args = {...}
  125. local function readFile(filePath)--Modified a bit for this specific purpose
  126.   local f = fs.open(filePath,"r")
  127.   local lineNum = 0
  128.   local fileLines = {}
  129.   repeat
  130.     lineNum = lineNum+1
  131.     fileLines[lineNum] = f.readLine()
  132.   until fileLines[lineNum] == "Below is the program that will decompress this"
  133.   f.close()
  134.   table.remove(fileLines,#fileLines)
  135.   return fileLines
  136. end
  137. if not args[1] or args[1] == "help" then
  138.   print("This file was made with Craft Compress by HPWebcamAble")
  139.   print("It will automagically uncompress itself!")
  140.   print("Usage:")
  141.   print(shell.getRunningProgram().." <destination path>")
  142. else
  143.   args[1] = shell.resolve(args[1])
  144.   if fs.exists(args[1]) then
  145.     printError("The destination already exists")
  146.     print("Overwrite?")
  147.     print("y/n")
  148.     while true do
  149.       local event,key = os.pullEvent("key")
  150.       if key == keys.y then
  151.         print("Will overwrite...")
  152.         os.pullEvent("char")
  153.         break
  154.       elseif key == keys.n then
  155.         printError("Canceled")
  156.         error()
  157.       end
  158.     end
  159.   end
  160.   print("Decompressing to "..args[1].."...")
  161.   local fLine = 4
  162.   local file = readFile(shell.resolve(shell.getRunningProgram()))
  163.   while fLine < #file do
  164.     f = fs.open(args[1]..file[fLine],"w")
  165.     fLine = fLine + 2
  166.     for i = 1, file[fLine-1] do
  167.       f.write(file[fLine].."\n")
  168.       fLine = fLine + 1
  169.     end
  170.     f.close()
  171.   end
  172.   print("Done!")
  173. end
  174.     ]])
  175.     f.close()
  176.     print("Done!")
  177.   end
  178. end
  179.  
  180. --Program--
  181.  
  182. local state,err = pcall(main)
  183.  
  184. if err then
  185.   if err:find("Terminated") then
  186.     printError("Terminated")
  187.   else
  188.     printError("Unexpected error, program stopped")
  189.     print(err)
  190.   end
  191. end
  192.  
  193. if f then f.close() end --Cleanup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement