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
- There are two versions
- One that also decompresses the files
- The other creates files that extract themselves
- This is the second one
- (Pastebin code for the first: YbyvQDP6)
- ==== Documentation ====
- ComputerCraft Forum Post:
- http://www.computercraft.info/forums2/index.php?/topic/22198-craft-compress/
- ==== Installation and Use ====
- Pastebin Code: JCCNWT8N
- Run this in a computer to download from pastebin
- pastebin get JCCNWT8N cc
- 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
- |2.0| <-- This program
- -Compressed files are extracted by running them (Like a program)
- |1.0|
- -Release
- ]]
- args = {...}
- local file = {}
- local compressionVersion = 2
- 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
- --This is the function that actually compresses the directroy
- --It needs to be a function because it calls itself
- 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")
- print("More info is in the file, type 'edit cc' to view")
- elseif args[1] == "help" or not args[2] then --help
- print("Usages:")
- print("cc path destination")
- print("Note: The file that is created uncompresses itself, just run it like a program")
- else --otherwise
- args[1] = shell.resolve(args[1])
- args[2] = shell.resolve(tostring(args[2]))
- if not fs.isDir(args[1]) then --Check stuff is good
- printError("The path must be a directory")
- print("Usage:")
- print("cc path destination")
- return
- elseif fs.exists(args[2]) 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[2],"w")
- if not f then
- printError("Unable to open destination path")
- return
- end
- table.insert(file,"--[[")
- table.insert(file,"This was created with Craft Compress by HPWebcamAble")
- table.insert(file,"Run it to decompress")
- print("Compressing "..args[1].." to "..args[2].."...")
- compressPath(args[1],"") --Actually does the compressing
- table.insert(file,"Below is the program that will decompress this")
- table.insert(file,"]]")
- for i = 1, #file do
- f.write(file[i].."\n")
- end
- f.write([[
- args = {...}
- local function readFile(filePath)--Modified a bit for this specific purpose
- local f = fs.open(filePath,"r")
- local lineNum = 0
- local fileLines = {}
- repeat
- lineNum = lineNum+1
- fileLines[lineNum] = f.readLine()
- until fileLines[lineNum] == "Below is the program that will decompress this"
- f.close()
- table.remove(fileLines,#fileLines)
- return fileLines
- end
- if not args[1] or args[1] == "help" then
- print("This file was made with Craft Compress by HPWebcamAble")
- print("It will automagically uncompress itself!")
- print("Usage:")
- print(shell.getRunningProgram().." <destination path>")
- else
- args[1] = shell.resolve(args[1])
- if fs.exists(args[1]) 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
- print("Decompressing to "..args[1].."...")
- local fLine = 4
- local file = readFile(shell.resolve(shell.getRunningProgram()))
- while fLine < #file do
- f = fs.open(args[1]..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!")
- end
- ]])
- f.close()
- print("Done!")
- 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