Advertisement
1lann

zip

Jan 19th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. -- MOZILLA PRODUCT --
  2. -- ZIP PROGRAM TO CONVERT A FOLDER TO A FILE --
  3.  
  4. local tArgs = {...}
  5.  
  6. print("Mozilla Zipper")
  7.  
  8. -- folder is the folder you want to zip
  9. function zipFolder(folder)
  10.     local folder = folder
  11.     local folderName = ""
  12.     if #fs.list(folder) < 1 then
  13.         error("Folder cannot be empty!")
  14.     end
  15.     local tData = {}
  16.     local tNames = {}
  17.     local tIsFolder = {}
  18.  
  19.     for k,b in pairs(fs.list(folder)) do
  20.         local v = (folder .."/" .. b)
  21.         if fs.isDir(v) then
  22.             table.insert(tIsFolder, true)
  23.             if #fs.list(v) < 1 then
  24.                 table.insert(tData, 1)
  25.             else
  26.                 table.insert(tData, zipFolder(v, tData, tNames, tIsFolder))
  27.             end
  28.         else
  29.             local f= io.open(v, "r")
  30.             table.insert(tData, f:read("*a"))
  31.             f:close()
  32.             table.insert(tIsFolder, false)
  33.         end
  34.         table.insert(tNames, b)
  35.     end
  36.     sData = textutils.serialize(tData)
  37.     sIsFolder = textutils.serialize(tIsFolder)
  38.     sNames = textutils.serialize(tNames)
  39.     if folder:find("/") then
  40.     local folderLoc = (folder:reverse():find("/")-1)
  41.     folderName = (folder:reverse():sub(1, folderLoc):reverse())
  42.     else
  43.     folderName = folder
  44.     end
  45.     local endData = textutils.serialize({folderName, sData, sIsFolder, sNames})
  46.     return endData
  47. end
  48.  
  49. if #tArgs == 2 then
  50.     if fs.exists(tArgs[1]) then
  51.         if fs.isDir(tArgs[1]) then
  52.             if not fs.exists(tArgs[2]) then
  53.                 print("Zipping...")
  54.                 local fileData = zipFolder(tArgs[1])
  55.                 local f = fs.open(tArgs[2], "w")
  56.                 f.write(fileData)
  57.                 f.close()
  58.                 print("Folder zipped to " .. tArgs[2])
  59.             else
  60.                 print("File already exists")
  61.             end
  62.         else
  63.             print("Not a folder")
  64.         end
  65.     else
  66.         print("Folder does not exist")
  67.     end
  68. else
  69.     print("Usage: zip <folder> <save-to>")
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement