Advertisement
einsteinK

CCraft - Zipper

May 2nd, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. local mode,path = ...
  2.  
  3. local function usage()
  4.     print("Usage:")
  5.     print("   zipper zip <folder>")
  6.     print("   zipper unzip <file>")
  7.     print("   zipper reinstall")
  8. end
  9.  
  10. local function zip(pre,path,name,hand)
  11.     print('zip(',pre,',',path,',',name,',h)')
  12.     local tot = fs.combine(path,name)
  13.     for k,v in pairs(fs.list(tot)) do
  14.         local p = fs.combine(tot,v)
  15.         if fs.isDir(p) then
  16.             local n = fs.combine(pre,v)
  17.             print("Directory ",v," of ",name," --> ",n)
  18.             hand.write(#n.." "..n.."D")
  19.             zip(fs.combine(pre,v),tot,v,hand)
  20.         else
  21.             --print("File ",v)
  22.             local n = fs.combine(pre,v)
  23.             hand.write(#n.." "..n.."F")
  24.             local f = fs.open(p,"r")
  25.             f = f.readAll(),f.close()
  26.             hand.write(#f.." "..f)
  27.         end
  28.     end
  29. end
  30.  
  31. local function put(par,file,source)
  32.     file = fs.combine(par,file)
  33.     if not fs.exists(par) then
  34.         fs.makeDir(par)
  35.     end
  36.     local h = fs.open(file,"w")
  37.     h.write(source) h.close()
  38. end
  39.  
  40. local function reader(str)
  41.     return {
  42.         match = function(s)
  43.             local r = str:match("^"..s)
  44.             if not r then return end
  45.             str = str:sub(#r+1) return r
  46.         end;
  47.         length = function(s)
  48.             local r = str:sub(1,s)
  49.             str = str:sub(s+1) return r
  50.         end;
  51.         one = function(s)
  52.             local r = str:sub(1,1)
  53.             str = str:sub(2) return r
  54.         end;
  55.     }
  56. end
  57.  
  58. if mode == "zip" then
  59.     if not path then return usage() end
  60.     path = shell.resolve(path)
  61.     if not fs.isDir(path) then
  62.         error("Not a directory",0)
  63.     end
  64.     local name = fs.getName(path)..".zpr"
  65.     local folder = path:sub(1,-#name+3)
  66.     folder = folder == "root" and "" or folder
  67.     local tot = shell.resolve(name)
  68.     if fs.exists(tot) then
  69.         error(name.." already exists",0)
  70.     end
  71.     local h = fs.open(tot,"w")
  72.     zip("",folder,fs.getName(path),h)
  73.     h.close()
  74.     print("Created ",name)
  75. elseif mode == "unzip" then
  76.     if not path then return usage() end
  77.     path = shell.resolve(path)
  78.     if not fs.exists(path) then
  79.         path = path..".zpr"
  80.     end
  81.     if not fs.exists(path) then
  82.         error("File doesn't exist",0)
  83.     elseif fs.isDir(path) then
  84.         error("Not a file",0)
  85.     end
  86.     local h = fs.open(path,"r")
  87.     h = h.readAll(),h.close()
  88.     h = reader(h)
  89.     local name = fs.getName(path)
  90.     name = name:match("%w+")
  91.     local par = fs.combine(path,"..")
  92.     par = fs.combine(par,name)
  93.     local length = h.match("(%d+) ")
  94.     length = tonumber(length) h.one()
  95.     while length do
  96.         local name = h.length(length)
  97.         local t = h.one()
  98.         local f = t == "D" and "folder" or "file"
  99.         print("  Unpacking ",f," ",name)
  100.         if t == "D" then
  101.             fs.makeDir(fs.combine(par,name))
  102.         elseif t == "F" then
  103.             length = tonumber(h.match("(%d+) ")) h.one()
  104.             put(par,name,h.length(length))
  105.         else
  106.             error"???"
  107.         end length = h.match("(%d+) ") h.one()
  108.     end print("Created")
  109. elseif mode == "reinstall" then
  110.     fs.delete(shell.getRunningProgram())
  111.     shell.run("pastebin get f8NGBm6w /"..shell.getRunningProgram())
  112. else
  113.     usage()
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement