Advertisement
1lann

archiver

Aug 3rd, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.68 KB | None | 0 0
  1. -- ZIPPER --
  2. -- VERIFICATION STAMP: Z1p 4rch1v3 [4PPR0V3D] --
  3.  
  4. local tArgs = { ... }
  5. workingDir = ("/.ziptool")
  6.  
  7. function getDir(dir)
  8.     local slashLoc = nil
  9.     for i = 2, string.len(dir) do
  10.         local b = (i*-1)
  11.         if string.sub(dir, b, b) == "/" then
  12.             slashLoc = (string.len(dir) - i)
  13.             local eDir = string.sub(dir, 1, slashLoc)
  14.             local eName = string.sub(dir, slashLoc+1, -1)
  15.             return eDir, eName
  16.         end
  17.     end
  18.     return nil
  19. end
  20.  
  21. function zipDir(toZip, saveTo)
  22. local f = nil
  23. local fTable = {}
  24. local nFiles = 0
  25. local nFolders = 0
  26. local nFileNames = {}
  27. local fData = nil
  28. local skip = false
  29. local listFiles = fs.list(toZip)
  30.     if listFiles[1] == nil then
  31.         return
  32.     else
  33.         for i = 1, #listFiles do
  34.             if listFiles[i] == "disk" and toZip == "/" and fs.isDir("disk") then
  35.                 local skip = true
  36.             elseif listFiles[i] == "disk2" and toZip == "/" and fs.isDir("disk2") then
  37.                 local skip = true
  38.             elseif listFiles[i] == "disk3" and toZip == "/" and fs.isDir("disk3") then
  39.                 local skip = true
  40.             elseif listFiles[i] == "rom" and toZip == "/" then
  41.                 local skip = true
  42.             else
  43.                 local skip = false
  44.             end
  45.             if not fs.isDir(toZip .. listFiles[i]) then
  46.                 table.insert(nFileNames, listFiles[i])
  47.                 nFiles = nFiles+1
  48.                 f = io.open(toZip, "r")
  49.                 fData = f:read("*a")
  50.                 f:close()
  51.                 table.insert(fTable, fData)
  52.             elseif not skip then
  53.                 print("Folder")
  54.                 table.insert(nFileNames, listFiles[i])
  55.                 nFolders = nFolders+1
  56.                 rFile = zipDir(toZip .. listFiles[i] .. "/", workingDir)
  57.                 f = io.open(rFile, "r")
  58.                 fData = f:read("*a")
  59.                 f:close()
  60.                 table.insert(fTable, fData)
  61.                 print("Folder Success")
  62.             end
  63.         end
  64.         table.insert(fTable, 1, "Z1p 4rch1v3 [4PPR0V3D]")
  65.         local fNames = textutils.serialize(nFileNames)
  66.         local fWrite = textutils.serialize(fTable)
  67.         if saveTo == nil then
  68.             saveTo = toZip
  69.         end
  70.         f = io.open(toZip .. ".zip", "w")
  71.         f:write(fWrite .. "\n" .. fNames)
  72.         f:close()
  73.         return (toZip .. ".zip"), nFiles, nFolders
  74.     end
  75.  
  76.  
  77.  
  78.  
  79. end
  80.  
  81. if not fs.exists("/.ziptool") then
  82.     fs.makeDir("/.ziptool")
  83. end
  84. if #tArgs > 0 and #tArgs < 3 then
  85.     print("Zipping...")
  86.     local toZip = nil
  87.     if string.sub(tArgs[1], string.len(tArgs[1]), string.len(tArgs[1])) == "/" then
  88.         toZip = tArgs[1]
  89.     else
  90.         toZip = (tArgs[1] .. "/")
  91.     end
  92.     if tArgs[2] then
  93.     if string.sub(tArgs[2], string.len(tArgs[2]), string.len(tArgs[2])) == "/" then
  94.         saveTo = tArgs[2]
  95.     else
  96.         saveTo = (tArgs[2] .. "/")
  97.     end
  98.     end
  99.     zipFile, nFiles, nFolders = zipDir(toZip, saveTo)
  100.     print("Zipped " .. nFiles .. " files and " .. nFolders .. " folders")
  101.     print("Zipped as " .. zipFile)
  102. else
  103. print("Usage: <command> <target> [savein]")
  104. print("Ex: ziptool MyFolder SaveHere")
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement