Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pressFile(IN)
- file = fs.open("TestFile","r")
- RGW = file.readAll()
- file.close()
- return RGW
- end
- function pressFolder(IN,en)
- PressTable = {}
- PressTable.Name = IN
- PressTable.files = {}
- --Checks
- if IN == nil then
- IN = "PressedFolder"
- end
- if en then
- GenKey(IN..".key")
- PressTable.PRESSKEY = IN..".key"
- else
- end
- if fs.exists(IN) and fs.isDir(IN) then
- print("pressing folder")
- local MainDir = fs.list(IN)
- local curDir = IN.."/"
- local curItem = ""
- for i = 1,#MainDir do
- curItem = curDir..MainDir[i]
- print("pressing "..curItem)
- if fs.isDir(curItem) then
- PressTable.files[MainDir[i]] = {}
- local TableLoc = PressTable.files[MainDir[i]]
- pressSubFolder(TableLoc,curItem,en)
- else
- file = fs.open(curItem,"r")
- if en then
- PressTable.files[MainDir[i]] = encrypt(file.readAll(),curItem,IN..".key")
- else
- PressTable.files[MainDir[i]] = file.readAll()
- end
- file.close()
- end
- sleep(.1)
- term.clear()
- term.setCursorPos(1,1)
- end
- file = fs.open(IN..".pressed","w")
- file.write(textutils.serialize(PressTable))
- file.close()
- print("Pressed to "..IN..".pressed")
- else
- print("Can not press folder")
- return
- end
- end
- function pressSubFolder(TableLoc,folder,en)
- local MainDir = fs.list(folder.."/")
- local curDir = folder.."/"
- local curItem = ""
- print("pressing subfolder "..folder)
- for i = 1,#MainDir do
- curItem = curDir..MainDir[i]
- print("pressing "..curItem)
- if fs.isDir(curItem) then
- TableLoc[MainDir[i]] = {}
- pressSubFolder(TableLoc[MainDir[i]],curItem,en)
- else
- file = fs.open(curItem,"r")
- if en then
- TableLoc[MainDir[i]] = encrypt(file.readAll(),curItem,PressTable.PRESSKEY)
- else
- TableLoc[MainDir[i]] = file.readAll()
- end
- file.close()
- end
- sleep(.1)
- term.clear()
- term.setCursorPos(1,1)
- end
- print("subfolder "..folder.." done")
- return
- end
- function GenKey(OUT)
- print("generating key...")
- KeyTable = {}
- for i = 1,127 do
- KeyTable[string.byte(string.char(i))] = math.random(1000,9999)
- print(string.char(i)..":"..KeyTable[string.byte(string.char(i))])
- sleep(.001)
- end
- print("Saving key to "..OUT)
- kf = fs.open(OUT,"w")
- kf.write(textutils.serialize(KeyTable))
- kf.close()
- print("Done.")
- end
- function unpackFolder(TABLE,key)
- if fs.exists(TABLE) and not fs.isDir(TABLE) then
- file = fs.open(TABLE,"r")
- PressTable = textutils.unserialize(file.readAll())
- file.close()
- if PressTable.PRESSKEY ~= nil and key == nil then
- print("Table is locked")
- return
- else
- if key ~= nil then
- en = true
- local KeyFile = fs.open(key,"r")
- key = textutils.unserialize(KeyFile.readAll())
- KeyFile.close()
- else
- en = false
- end
- print("unpacking folder")
- fs.makeDir(PressTable.Name)
- print("Created folder "..PressTable.Name)
- local MainDir = PressTable.Name.."/"
- local curDir = MainDir
- local curItem = ""
- for name in pairs(PressTable.files) do
- curItem = curDir..name
- print("unpacking "..type(PressTable.files[name]).." "..curItem)
- if type(PressTable.files[name]) == "table" then
- fs.makeDir(curItem)
- unpackSubFolder(PressTable.files[name],curItem,key,en)
- else
- file = fs.open(curItem,"w")
- if en then
- file.write(decrypt(PressTable.files[name],key))
- else
- file.write(PressTable.files[name])
- end
- file.close()
- end
- sleep(.1)
- term.clear()
- term.setCursorPos(1,1)
- end
- print("Done unpacking")
- return
- end
- end
- end
- function unpackSubFolder(TABLE,loc,key,en)
- local MainDir = PressTable.Name.."/"
- local curDir = loc
- local curItem = ""
- for name in pairs(TABLE) do
- curItem = curDir.."/"..name
- print("unpacking subfolder "..type(TABLE[name]).." "..curItem)
- if type(TABLE[name]) == "table" then
- fs.makeDir(curItem)
- unpackSubFolder(TABLE[name],curItem,key,en)
- else
- file = fs.open(curItem,"w")
- if en then
- file.write(decrypt(TABLE[name],key))
- else
- file.write(TABLE[name])
- end
- file.close()
- end
- sleep(.1)
- term.clear()
- term.setCursorPos(1,1)
- end
- print("subfolder "..loc.." done")
- end
- function encrypt(S,NAME,key)
- local KeyFile = fs.open(key,"r")
- local key = textutils.unserialize(KeyFile.readAll())
- KeyFile.close()
- local encryptedString = ""
- curPer = 0
- for i = 1,string.len(S) do
- curPer = math.floor((i/string.len(S))*100)
- if key[string.byte(string.sub(S,i,i))] then
- encryptedString = encryptedString..key[string.byte(string.sub(S,i,i))]
- else
- print("unknown code found! "..string.byte(string.sub(S,i,i)))
- sleep(1)
- end
- sleep(.00000000001)
- term.clear()
- term.setCursorPos(1,1)
- write("encrypting "..NAME.." "..curPer.."%")
- W,H = term.getSize()
- term.setCursorPos(1,H)
- if term.isColor then
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.green)
- end
- for q = 1,(curPer/100)*W do
- write("|")
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- return encryptedString
- end
- function decrypt(encryptedString,key)
- local code = ""
- local S = ""
- for i = 1,string.len(encryptedString) do
- code = code..string.sub(encryptedString,i,i)
- if string.len(code) == 4 then
- for keys in pairs(key) do
- if tostring(key[keys]) == tostring(code) then
- S = S..string.char(keys)
- end
- end
- code = ""
- if string.len(encryptedString) > 500 then
- sleep(.0000000000001)
- end
- end
- end
- return S
- end
- args = {...}
- term.clear()
- term.setCursorPos(1,1)
- if args[1] == "press" then
- pressFolder(args[2],args[3])
- elseif args[1] == "unpack" then
- unpackFolder(args[2],args[3])
- end
Advertisement
Add Comment
Please, Sign In to add comment