Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- HPWebcamAble Presents...
- Craft Compress
- === Description ====
- Craft Compress makes directories into a single file
- And back again. Duh
- ==== Documentation ====
- ComputerCraft Forum Post:
- http://www.computercraft.info/forums2/index.php?/topic/22198-craft-compress/
- ==== Installation and Use ====
- Pastebin Code: YbyvQDP6
- Download this file onto the computer you want to use it on
- Type 'cc help' for usages
- ==== Update History ====
- The pastebin will always have the most recent version
- |1.0| <-- This program
- -Release
- ]]
- args = {...}
- local file = {}
- local compressionVersion = 1
- local function readFile(filePath)
- local f = fs.open(filePath,"r")
- local lineNum = 0
- local fileLines = {}
- repeat
- lineNum = lineNum+1
- fileLines[lineNum] = f.readLine()
- until fileLines[lineNum] == nil
- f.close()
- return fileLines
- end
- local function compressPath(path,iPath)
- path = shell.resolve(path)
- local dir = fs.list(path)
- for i = 1, #dir do
- if fs.isDir(path.."/"..dir[i]) then
- compressPath(path.."/"..dir[i],iPath.."/"..dir[i])
- else
- local tempFile = {}
- table.insert(file,iPath.."/"..dir[i])
- local tempFile = readFile(path.."/"..dir[i])
- table.insert(file,#tempFile)
- for i = 1, #tempFile do
- table.insert(file,tempFile[i])
- end
- end
- end
- end
- local function main()
- if #args == 0 then --No arguments
- print("Craft Compress takes a directory and makes it into a single file")
- print("Type 'cc help' for usages")
- elseif args[1] == "c" then --Compress
- args[2] = shell.resolve(args[2])
- if not fs.isDir(args[2]) then
- printError("The path must be a directory")
- print("Usage:")
- print("cc c path destination")
- return
- elseif fs.exists(args[3]) then
- printError("Destination exists")
- print("Overwrite? y/n")
- while true do
- local event,key = os.pullEvent("key")
- if key == keys.y then
- print("Will overwrite...")
- break
- elseif key == keys.n then
- printError("Canceled")
- error()
- end
- end
- end
- f = fs.open(args[3],"w")
- if not f then
- printError("Unable to open destination path")
- return
- end
- table.insert(file,"This was created with Craft Compress by HPWebcamAble")
- table.insert(file,"COMPRESSION VERSION:")
- table.insert(file,"1")
- print("Compressing "..args[2].." to "..args[3].."...")
- compressPath(args[2],"")
- for i = 1, #file do
- f.write(file[i].."\n")
- end
- f.close()
- print("Done!")
- elseif args[1] == "u" then --Decompress
- args[2] = shell.resolve(args[2])
- if not args[3] then
- printError("Expected destination")
- print("Usage:")
- print("cc u path destination")
- return
- elseif not fs.exists(args[2]) then
- printError("That file doesn't exsit")
- return
- elseif fs.isDir(args[2]) then
- printError("That's a directory. Only FILES that were created with this program can be uncompressed")
- return
- elseif fs.exists(args[3]) then
- printError("The destination already exists")
- print("Overwrite?")
- print("y/n")
- while true do
- local event,key = os.pullEvent("key")
- if key == keys.y then
- print("Will overwrite...")
- os.pullEvent("char")
- break
- elseif key == keys.n then
- printError("Canceled")
- error()
- end
- end
- end
- file = readFile(args[2])
- if file[1] ~= "This was created with Craft Compress by HPWebcamAble" then
- printError("This file wasn't made with Craft Compress")
- return
- elseif tonumber(file[3]) ~= compressionVersion then
- printError("This file was compressed with a different version of Craft Compress")
- return
- end
- print("Decompressing "..args[2].." to "..args[3].."...")
- local fLine = 4
- while fLine < #file do
- f = fs.open(args[3]..file[fLine],"w")
- fLine = fLine + 2
- for i = 1, file[fLine-1] do
- f.write(file[fLine].."\n")
- fLine = fLine + 1
- end
- f.close()
- end
- print("Done!")
- else
- print("Usages:")
- print("cc c path destination")
- print("cc u path destination")
- print("c Compresses")
- print("u Decompresses")
- end
- end
- --Program--
- local state,err = pcall(main)
- if err then
- if err:find("Terminated") then
- printError("Terminated")
- else
- printError("Unexpected error, program stopped")
- print(err)
- end
- end
- if f then f.close() end --Cleanup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement