Advertisement
HPWebcamAble

[CC][1.0] Craft Compress

Feb 28th, 2015
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.57 KB | None | 0 0
  1. --[[
  2. HPWebcamAble Presents...
  3. Craft Compress
  4.  
  5. === Description ====
  6. Craft Compress makes directories into a single file
  7. And back again. Duh
  8.  
  9.  
  10. ==== Documentation ====
  11. ComputerCraft Forum Post:
  12. http://www.computercraft.info/forums2/index.php?/topic/22198-craft-compress/
  13.  
  14.  
  15. ==== Installation and Use ====
  16. Pastebin Code: YbyvQDP6
  17.  
  18. Download this file onto the computer you want to use it on
  19. Type 'cc help' for usages
  20.  
  21.  
  22. ==== Update History ====
  23. The pastebin will always have the most recent version
  24.  
  25. |1.0| <-- This program
  26.   -Release
  27. ]]
  28.  
  29. args = {...}
  30. local file = {}
  31. local compressionVersion = 1
  32.  
  33. local function readFile(filePath)
  34.   local f = fs.open(filePath,"r")
  35.   local lineNum = 0
  36.   local fileLines = {}
  37.   repeat
  38.     lineNum = lineNum+1
  39.     fileLines[lineNum] = f.readLine()
  40.   until fileLines[lineNum] == nil
  41.   f.close()
  42.   return fileLines
  43. end
  44.  
  45. local function compressPath(path,iPath)
  46.   path = shell.resolve(path)
  47.   local dir = fs.list(path)
  48.   for i = 1, #dir do
  49.     if fs.isDir(path.."/"..dir[i]) then
  50.       compressPath(path.."/"..dir[i],iPath.."/"..dir[i])
  51.     else
  52.       local tempFile = {}
  53.       table.insert(file,iPath.."/"..dir[i])
  54.       local tempFile = readFile(path.."/"..dir[i])
  55.       table.insert(file,#tempFile)
  56.       for i = 1, #tempFile do
  57.         table.insert(file,tempFile[i])
  58.       end
  59.     end
  60.   end
  61. end
  62.  
  63. local function main()
  64.   if #args == 0 then --No arguments
  65.     print("Craft Compress takes a directory and makes it into a single file")
  66.     print("Type 'cc help' for usages")
  67.   elseif args[1] == "c" then --Compress
  68.     args[2] = shell.resolve(args[2])
  69.     if not fs.isDir(args[2]) then
  70.       printError("The path must be a directory")
  71.       print("Usage:")
  72.       print("cc c path destination")
  73.       return
  74.     elseif fs.exists(args[3]) then
  75.       printError("Destination exists")
  76.       print("Overwrite? y/n")
  77.       while true do
  78.         local event,key = os.pullEvent("key")
  79.         if key == keys.y then
  80.           print("Will overwrite...")
  81.           break
  82.         elseif key == keys.n then
  83.           printError("Canceled")
  84.           error()
  85.         end
  86.       end
  87.     end
  88.     f = fs.open(args[3],"w")
  89.     if not f then
  90.       printError("Unable to open destination path")
  91.       return
  92.     end
  93.     table.insert(file,"This was created with Craft Compress by HPWebcamAble")
  94.     table.insert(file,"COMPRESSION VERSION:")
  95.     table.insert(file,"1")
  96.     print("Compressing "..args[2].." to "..args[3].."...")
  97.     compressPath(args[2],"")
  98.     for i = 1, #file do
  99.       f.write(file[i].."\n")
  100.     end
  101.     f.close()
  102.     print("Done!")
  103.   elseif args[1] == "u" then --Decompress
  104.     args[2] = shell.resolve(args[2])
  105.     if not args[3] then
  106.       printError("Expected destination")
  107.       print("Usage:")
  108.       print("cc u path destination")
  109.       return
  110.     elseif not fs.exists(args[2]) then
  111.       printError("That file doesn't exsit")
  112.       return
  113.     elseif fs.isDir(args[2]) then
  114.       printError("That's a directory. Only FILES that were created with this program can be uncompressed")
  115.       return
  116.     elseif fs.exists(args[3]) then
  117.       printError("The destination already exists")
  118.       print("Overwrite?")
  119.       print("y/n")
  120.       while true do
  121.         local event,key = os.pullEvent("key")
  122.         if key == keys.y then
  123.           print("Will overwrite...")
  124.           os.pullEvent("char")
  125.           break
  126.         elseif key == keys.n then
  127.           printError("Canceled")
  128.           error()
  129.         end
  130.       end
  131.     end
  132.     file = readFile(args[2])
  133.     if file[1] ~= "This was created with Craft Compress by HPWebcamAble" then
  134.       printError("This file wasn't made with Craft Compress")
  135.       return
  136.     elseif tonumber(file[3]) ~= compressionVersion then
  137.       printError("This file was compressed with a different version of Craft Compress")
  138.       return
  139.     end
  140.     print("Decompressing "..args[2].." to "..args[3].."...")
  141.     local fLine = 4
  142.     while fLine < #file do
  143.       f = fs.open(args[3]..file[fLine],"w")
  144.       fLine = fLine + 2
  145.       for i = 1, file[fLine-1] do
  146.         f.write(file[fLine].."\n")
  147.         fLine = fLine + 1
  148.       end
  149.       f.close()
  150.     end
  151.     print("Done!")
  152.   else
  153.     print("Usages:")
  154.     print("cc c path destination")
  155.     print("cc u path destination")
  156.     print("c Compresses")
  157.     print("u Decompresses")
  158.   end
  159. end
  160.  
  161. --Program--
  162.  
  163. local state,err = pcall(main)
  164.  
  165. if err then
  166.   if err:find("Terminated") then
  167.     printError("Terminated")
  168.   else
  169.     printError("Unexpected error, program stopped")
  170.     print(err)
  171.   end
  172. end
  173.  
  174. if f then f.close() end --Cleanup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement