Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Packager by CrazedProgrammer
- b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
- function tobase64(data)
- return ((data:gsub('.', function(x)
- local r,b='',x:byte()
- for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
- return r;
- end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
- if (#x < 6) then return '' end
- local c=0
- for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
- return b:sub(c+1,c+1)
- end)..({ '', '==', '=' })[#data%3+1])
- end
- function lzw_encode(str)
- local w = ''
- local result = {}
- local dict_size = 256
- local dict = {}
- for i = 0, dict_size-1 do
- dict[string.char(i)] = i
- end
- local i = dict_size
- for char in str:gmatch('.') do
- local wc = w .. char
- if dict[wc] then
- w = wc
- else
- table.insert(result, dict[w])
- dict[wc] = i
- i = i + 1
- w = char
- end
- end
- if w~='' then
- table.insert(result, dict[w])
- end
- local s = ""
- for k,v in pairs(result) do
- s = s..string.char(math.floor(v / 256))
- s = s..string.char(v % 256)
- end
- return s
- end
- function add(path)
- if fs.isDir(dir.."/"..path) then
- local files = 0
- for k,v in pairs(fs.list(dir.."/"..path)) do
- add(path.."/"..v)
- files = files + 1
- end
- if files == 0 then
- data = data..string.char(0)
- data = data..string.char(#path)
- data = data..path
- data = data..string.char(2)
- end
- elseif path ~= "" then
- local f = fs.open(dir.."/"..path, "r")
- local d = lzw_encode(f.readAll())
- data = data..string.char(0)
- data = data..string.char(#path)
- data = data..path
- data = data..string.char(1)
- data = data..string.char(math.floor(#d / 65536))
- data = data..string.char(math.floor(#d / 256) - math.floor(#d / 65536) * 256)
- data = data..string.char(#d % 256)
- data = data..d
- f.close()
- end
- end
- args = {...}
- if #args < 2 then print("Usage: package <directory> <output file> [display name]") return end
- dir = args[1]
- if dir:sub(1, 1) ~= "/" then
- dir = "/"..fs.getDir(shell.getRunningProgram()).."/"..dir
- end
- file = args[2]
- if file:sub(1, 1) ~= "/" then
- file = "/"..fs.getDir(shell.getRunningProgram()).."/"..file
- end
- fs.delete(file)
- name = args[3] or fs.getName(dir)
- if not fs.isDir(dir) then print("Directory "..dir.." doesn't exist") return end
- data = ""
- add("")
- 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..'.')"
- local f = fs.open(file, "w")
- f.write(program)
- f.close()
Add Comment
Please, Sign In to add comment