Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fileHierarchy = {}
- local args = {...}
- -- fileHierarchy Structure:
- -- {["dir1"] = true, ["file1"] = {false, content}, ["dir1/file2"] = {false, content}, ["dir1/dir2"] = true }
- if fs.exists(args[1]) then
- local fileHandle = fs.open(args[1], "r")
- local fileHierarchyString = fileHandle.readAll()
- fileHierarchy = textutils.unserialize(fileHierarchyString)
- fileHandle.close()
- end
- local function unpackZip(destination)
- for i,v in pairs(fileHierarchy) do
- if v == true then
- fs.makeDir( fs.combine(destination, i) )
- else
- local namestart = string.find(i, fs.getName(i))
- namestart = namestart-1
- local basePath = string.sub(i, 1, namestart)
- if not fs.exists(fs.combine(destination, basePath)) then
- fs.makeDir(fs.combine(destination, basePath))
- end
- local handle = fs.open( fs.combine(destination, i) , "w")
- handle.write(v)
- handle.close()
- end
- end
- end
- local function flushZip()
- local fileHandle = fs.open(args[1], "w")
- fileHandle.write( textutils.serialize(fileHierarchy) )
- fileHandle.close()
- end
- local function addDirectory(directory)
- if fileHierarchy[directory] == nil then
- fileHierarchy[directory] = true
- return true
- else
- return false
- end
- end
- local function addFile(localFilePath, zipFilePath)
- if fs.exists(localFilePath) and fileHierarchy[zipFilePath] == nil then
- local handle = fs.open(localFilePath, "r")
- fileHierarchy[zipFilePath] = handle.readAll()
- handle.close()
- return true
- else
- return false
- end
- end
- local function moveFile(oldFilePath, newFilePath)
- if fileHierarchy[oldFilePath] ~= nil then
- fileHierarchy[newFilePath] = fileHierarchy[oldFilePath]
- fileHierarchy[oldFilePath] = nil
- return true
- else
- return false
- end
- end
- local function copyFile(oldFilePath, newFilePath)
- if fileHierarchy[oldFilePath] ~= nil then
- fileHierarchy[newFilePath] = fileHierarchy[oldFilePath]
- return true
- else
- return false
- end
- end
- local function deleteFile(filePath)
- if fileHierarchy[filePath] ~= nil then
- fileHierarchy[filePath] = nil
- return true
- else
- return false
- end
- end
- local function showMenu()
- while true do
- term.clear()
- term.setCursorPos(1,1)
- io.write("Package Manager V1")
- term.setCursorPos(1,2)
- io.write("1 - Add File")
- term.setCursorPos(1,3)
- io.write("2 - Delete File")
- term.setCursorPos(1,4)
- io.write("3 - Copy File")
- term.setCursorPos(1,5)
- io.write("4 - Move File")
- term.setCursorPos(1,6)
- io.write("5 - Add Directory")
- term.setCursorPos(1,7)
- io.write("6 - Add Existing Directory")
- term.setCursorPos(1,8)
- io.write("7 - List Files in Package")
- term.setCursorPos(1,9)
- io.write("8 - Extract Package")
- term.setCursorPos(1,10)
- io.write("9 - Exit")
- local maxX, maxY = term.getSize()
- term.setCursorPos(1, maxY-1)
- io.write("Open:")
- term.setCursorPos(1, maxY)
- io.write(args[1])
- term.setCursorPos(1, 11)
- local event, key = os.pullEvent("key")
- if key == 2 then
- term.setCursorBlink(true)
- print("Please type in the file's path on your computer. >")
- local filePath = io.read()
- print("Please type in where the file should be located in the package. >")
- local zipPath = io.read()
- if addFile(filePath, zipPath) then
- print("Operation successful.")
- else
- print("Operation failed. Does the file exist?")
- end
- term.setCursorBlink(false)
- elseif key == 3 then
- term.setCursorBlink(true)
- io.write("Please type in the path to the file to be deleted. >")
- local filePath = io.read()
- if deleteFile(filePath) then
- print("Operation successful.")
- else
- print("Operation failed. Does the file exist?")
- end
- term.setCursorBlink(false)
- elseif key == 4 then
- term.setCursorBlink(true)
- print("Please type in the file's current path. >")
- local filePath = io.read()
- print("Please type in where the file will be copied to. >")
- local copyPath = io.read()
- if copyFile(filePath, copyPath) then
- print("Operation successful.")
- else
- print("Operation failed. Does the file exist?")
- end
- term.setCursorBlink(false)
- elseif key == 5 then
- term.setCursorBlink(true)
- print("Please type in the file's current path. >")
- local filePath = io.read()
- print("Please type in where the file will be moved to. >")
- local movePath = io.read()
- if moveFile(filePath, movePath) then
- print("Operation successful.")
- else
- print("Operation failed. Does the file exist?")
- end
- term.setCursorBlink(false)
- elseif key == 6 then
- term.setCursorBlink(true)
- print("Please type in the new directory's full path >")
- local dirPath = io.read()
- if addDirectory(dirPath) then
- print("Operation successful.")
- else
- print("Operation failed. Does the directory already exist?")
- end
- term.setCursorBlink(false)
- elseif key == 7 then
- term.setCursorBlink(true)
- print("Please type in the directory's full path >")
- local dirPath = io.read()
- if fs.exists(dirPath) and fs.isDir(dirPath) then
- for i,v in ipairs(fs.list(dirPath)) do
- if not addFile(fs.combine(dirPath,v), v) then
- print("Operation failed. Does the file exist?")
- break
- end
- end
- print("Operation successful.")
- end
- term.setCursorBlink(false)
- elseif key == 8 then
- term.clear()
- term.setCursorPos(1,1)
- for i,v in pairs(fileHierarchy) do
- print(i)
- end
- elseif key == 9 then
- term.setCursorBlink(true)
- print("Please type in where to extract the package to. >")
- local destination = io.read()
- if not fs.exists(destination) then
- fs.makeDir(destination)
- end
- unpackZip(destination)
- term.setCursorBlink(false)
- elseif key == 10 then
- flushZip()
- term.clear()
- term.setCursorPos(1,1)
- return
- end
- flushZip()
- if key ~= 8 then
- term.setCursorPos(1, maxY-2)
- else
- term.setCursorPos(1, maxY)
- end
- io.write("Press any key to continue.")
- os.pullEvent("key")
- end
- end
- showMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement