Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mode,path = ...
- local function usage()
- print("Usage:")
- print(" zipper zip <folder>")
- print(" zipper unzip <file>")
- print(" zipper reinstall")
- end
- local function zip(pre,path,name,hand)
- print('zip(',pre,',',path,',',name,',h)')
- local tot = fs.combine(path,name)
- for k,v in pairs(fs.list(tot)) do
- local p = fs.combine(tot,v)
- if fs.isDir(p) then
- local n = fs.combine(pre,v)
- print("Directory ",v," of ",name," --> ",n)
- hand.write(#n.." "..n.."D")
- zip(fs.combine(pre,v),tot,v,hand)
- else
- --print("File ",v)
- local n = fs.combine(pre,v)
- hand.write(#n.." "..n.."F")
- local f = fs.open(p,"r")
- f = f.readAll(),f.close()
- hand.write(#f.." "..f)
- end
- end
- end
- local function put(par,file,source)
- file = fs.combine(par,file)
- if not fs.exists(par) then
- fs.makeDir(par)
- end
- local h = fs.open(file,"w")
- h.write(source) h.close()
- end
- local function reader(str)
- return {
- match = function(s)
- local r = str:match("^"..s)
- if not r then return end
- str = str:sub(#r+1) return r
- end;
- length = function(s)
- local r = str:sub(1,s)
- str = str:sub(s+1) return r
- end;
- one = function(s)
- local r = str:sub(1,1)
- str = str:sub(2) return r
- end;
- }
- end
- if mode == "zip" then
- if not path then return usage() end
- path = shell.resolve(path)
- if not fs.isDir(path) then
- error("Not a directory",0)
- end
- local name = fs.getName(path)..".zpr"
- local folder = path:sub(1,-#name+3)
- folder = folder == "root" and "" or folder
- local tot = shell.resolve(name)
- if fs.exists(tot) then
- error(name.." already exists",0)
- end
- local h = fs.open(tot,"w")
- zip("",folder,fs.getName(path),h)
- h.close()
- print("Created ",name)
- elseif mode == "unzip" then
- if not path then return usage() end
- path = shell.resolve(path)
- if not fs.exists(path) then
- path = path..".zpr"
- end
- if not fs.exists(path) then
- error("File doesn't exist",0)
- elseif fs.isDir(path) then
- error("Not a file",0)
- end
- local h = fs.open(path,"r")
- h = h.readAll(),h.close()
- h = reader(h)
- local name = fs.getName(path)
- name = name:match("%w+")
- local par = fs.combine(path,"..")
- par = fs.combine(par,name)
- local length = h.match("(%d+) ")
- length = tonumber(length) h.one()
- while length do
- local name = h.length(length)
- local t = h.one()
- local f = t == "D" and "folder" or "file"
- print(" Unpacking ",f," ",name)
- if t == "D" then
- fs.makeDir(fs.combine(par,name))
- elseif t == "F" then
- length = tonumber(h.match("(%d+) ")) h.one()
- put(par,name,h.length(length))
- else
- error"???"
- end length = h.match("(%d+) ") h.one()
- end print("Created")
- elseif mode == "reinstall" then
- fs.delete(shell.getRunningProgram())
- shell.run("pastebin get f8NGBm6w /"..shell.getRunningProgram())
- else
- usage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement