Advertisement
Alakazard12

Zip/Unzip

Dec 16th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.16 KB | None | 0 0
  1. local function getName(path)
  2.     local newp = path
  3.     for i = 1, #path do
  4.         if string.sub(path, i, i) == "/" then
  5.             newp = string.sub(path, i+1)
  6.         end
  7.     end
  8.     return newp
  9. end
  10.  
  11. zip = {}
  12.  
  13. function zip.pack(file)
  14.     local czip = {}
  15.         local function getz(path, pon)
  16.         -- path = shell.resolve(path)
  17.         if fs.isDir(path) then
  18.             pon[getName(path)] = {}
  19.             for i,v in pairs(fs.list(path)) do
  20.                 getz(path.."/"..v, pon[getName(path)])
  21.             end
  22.         else
  23.             if shell.resolve(path) ~= "rom/programs/secret/alongtimeago" then
  24.                 local fg = fs.open(path, "r")
  25.                 pon[getName(path)] = fg.readAll()
  26.                 fg.close()
  27.             end
  28.         end
  29.     end
  30.     getz(file, czip)
  31.     return czip
  32. end
  33.  
  34. function zip.unpack(tbl, place)
  35.     local nm
  36.     for i,v in pairs(tbl) do
  37.         nm = i
  38.         tbl = v
  39.     end
  40.     fs.makeDir(place.."/"..nm)
  41.     file = place .. "/" .. nm
  42.     local function undz(pon, tln, tl)
  43.         if type(tl) == "table" then
  44.             fs.makeDir(pon.."/"..tln)
  45.             for i,v in pairs(tl) do
  46.                 undz(pon.."/"..tln, i, v)
  47.             end
  48.         elseif type(tl) == "string" then
  49.             local tr = fs.open(pon.."/"..tln, "w")
  50.             tr.write(tl)
  51.             tr.close()
  52.         end
  53.     end
  54.     for i,v in pairs(tbl) do
  55.         undz(file, i, v)
  56.     end
  57.     return true
  58. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement