guitarplayer616

packager

Jan 22nd, 2017
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Packager by CrazedProgrammer
  2.  
  3. b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  4. function tobase64(data)
  5.   return ((data:gsub('.', function(x)
  6.     local r,b='',x:byte()
  7.     for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  8.     return r;
  9.   end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  10.     if (#x < 6) then return '' end
  11.     local c=0
  12.     for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
  13.     return b:sub(c+1,c+1)
  14.   end)..({ '', '==', '=' })[#data%3+1])
  15. end
  16. function lzw_encode(str)
  17.   local w = ''
  18.   local result = {}
  19.   local dict_size = 256
  20.   local dict = {}
  21.   for i = 0, dict_size-1 do
  22.     dict[string.char(i)] = i
  23.   end
  24.   local i = dict_size
  25.   for char in str:gmatch('.') do
  26.     local wc = w .. char
  27.     if dict[wc] then
  28.       w = wc
  29.     else
  30.       table.insert(result, dict[w])
  31.       dict[wc] = i
  32.       i = i + 1
  33.       w = char
  34.     end
  35.   end
  36.   if w~='' then
  37.     table.insert(result, dict[w])
  38.   end
  39.   local s = ""
  40.   for k,v in pairs(result) do
  41.     s = s..string.char(math.floor(v / 256))
  42.     s = s..string.char(v % 256)
  43.   end
  44.   return s
  45. end
  46. function add(path)
  47.   if fs.isDir(dir.."/"..path) then
  48.     local files = 0
  49.     for k,v in pairs(fs.list(dir.."/"..path)) do
  50.       add(path.."/"..v)
  51.       files = files + 1
  52.     end
  53.     if files == 0 then
  54.       data = data..string.char(0)
  55.       data = data..string.char(#path)
  56.       data = data..path
  57.       data = data..string.char(2)
  58.     end
  59.   elseif path ~= "" then
  60.     local f = fs.open(dir.."/"..path, "r")
  61.     local d = lzw_encode(f.readAll())
  62.     data = data..string.char(0)
  63.     data = data..string.char(#path)
  64.     data = data..path
  65.     data = data..string.char(1)
  66.     data = data..string.char(math.floor(#d / 65536))
  67.     data = data..string.char(math.floor(#d / 256) - math.floor(#d / 65536) * 256)
  68.     data = data..string.char(#d % 256)
  69.     data = data..d
  70.     f.close()
  71.   end
  72. end
  73. args = {...}
  74. if #args < 2 then print("Usage: package <directory> <output file> [display name]") return end
  75. dir = args[1]
  76. if dir:sub(1, 1) ~= "/" then
  77.   dir = "/"..fs.getDir(shell.getRunningProgram()).."/"..dir
  78. end
  79. file = args[2]
  80. if file:sub(1, 1) ~= "/" then
  81.   file = "/"..fs.getDir(shell.getRunningProgram()).."/"..file
  82. end
  83. fs.delete(file)
  84. name = args[3] or fs.getName(dir)
  85. if not fs.isDir(dir) then print("Directory "..dir.." doesn't exist") return end
  86. data = ""
  87. add("")
  88. program = "--Created with Packager by CrazedProgrammer\nb='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' function frombase64(data) data = string.gsub(data, '[^'..b..'=]', '') return (data:gsub('.', function(x) if (x == '=') then return '' end local r,f='',(b:find(x)-1) for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end return r; end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x) if (#x ~= 8) then return '' end local c=0 for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end return string.char(c) end)) end function lzw_decode(s) local str = { } for i=1,#s/2,1 do table.insert(str, s:byte(i * 2 - 1) * 256 + s:byte(i * 2)) end local dict_size = 256 local dict = {} for i = 0, dict_size-1 do dict[i] = string.char(i) end local w = string.char(str[1]) local result = w for i = 2, #str do local k = str[i] local entry = '' if dict[k] then entry = dict[k] elseif k == dict_size then entry = w .. w:sub(1,1) else return nil end result = result .. entry dict[dict_size] = w .. entry:sub(1,1) dict_size = dict_size + 1 w = entry end return result end d = '"..tobase64(data).."' data = frombase64(d) print('Where do you want to extract "..name.."?') dir = read() if dir:sub(1,1) ~= '/' then dir = '/'..fs.getDir(shell.getRunningProgram())..'/'..dir end fs.delete(dir) local i = 1 local path = '' while i <= #data do local c = data:byte(i) if c == 0 then i = i + 1 local l = data:byte(i) path = dir..'/'..data:sub(i + 1, i + l) i = i + l + 1 elseif c == 1 then i = i + 3 local l = data:byte(i - 2) * 65536 + data:byte(i - 1) * 256 + data:byte(i) local f = fs.open(path, 'w') f.write(lzw_decode(data:sub(i + 1, i + l))) f.close() i = i + l + 1 elseif c == 2 then fs.makeDir(path) i = i + 1 end end print('Extraced to '..dir..'.')"
  89. local f = fs.open(file, "w")
  90. f.write(program)
  91. f.close()
Add Comment
Please, Sign In to add comment