Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local M = {}
- M.root = 'C:\\Program Files\\7-Zip\\7z.exe'
- local function fmt(s)
- if s:find(' ') and s:sub(1, 1) ~= '"' then
- return '"' .. s .. '"'
- end
- return s
- end
- function M.command(...)
- local data = 'CALL'
- for i = 1, select('#', ...) do
- data = data .. ' ' .. fmt(select(i, ...))
- end
- print('COMMAND [' .. data .. ']')
- return data
- end
- local File = {}
- File.__index = File
- function File:new(o)
- return setmetatable(o or {}, self)
- end
- function File:extract(dst)
- return os.execute(M.command(M.root, "x", "-y", self.src, self.path, '-o' .. dst))
- end
- function File:read(dst)
- if self.data then return self.data end
- local f = io.popen(M.command(M.root, 'x', '-so', self.src, self.path), 'rb')
- self.data = f:read("*a")
- f:close()
- return self.data
- end
- function M.list(src, filter)
- local files = {}
- local command = M.command(M.root, 'l', src, filter or '*')
- local listing = io.popen(command, 'rb')
- local nextLine = listing:lines()
- local line = nextLine()
- while line:sub(1, 2) ~= '--' do
- line = nextLine()
- end
- line = nextLine()
- while line:sub(1, 2) ~= '--' do
- local key, value = line:match('(.-) = (.*)')
- if key and value then
- files[key:lower():gsub('%W', '_')] = tonumber(value) or value
- end
- line = nextLine()
- end
- line = nextLine()
- while line:sub(1, 2) ~= '--' do
- local o = {}
- o.src = src
- o.date, o.time, o.res, o.size, o.compressed, o.path = line:match('(.-)%s+(.-)%s+(.-)%s+(.-)%s+(.-)%s+(.*)')
- if o.date then
- table.insert(files, File:new(o))
- end
- line = nextLine()
- end
- listing:close()
- return files
- end
- function M.extract(src, dst, filter)
- filter = filter or '*'
- return os.execute(M.command(M.root, "x", "-y", src, filter, '-o' .. dst))
- end
- function M.read(src, filter)
- filter = filter or '*'
- local list = M.list(src, filter)
- local file = io.popen(M.command(M.root, 'x', '-so', src, filter), 'rb')
- local data = file:read("*a"); file:close()
- local pos = 1
- for i, v in ipairs(list) do
- --print(pos, v.size)
- local filedata = data:sub(pos, pos + v.size - 1)
- pos = pos + v.size
- v.data = filedata
- end
- return list
- end
- if not ... then return M end
- function lines_iter(s, pos, d)
- print(s, pos, d)
- pos = pos + 1
- if pos > #s then return nil end
- local i, j = s:find('\n', pos)
- if not j then
- return #s, s:sub(pos)
- end
- local v = s:sub(pos, j - 1)
- return j, v
- end
- function string.lines(s)
- return lines_iter, s, 0
- end
- local time = os.clock()
- local files = M.read('C:\\7-Zip\\processed.zip', '*')
- local file = files[1004]
- print(file.data)
- print("Len", len, "Time", os.clock() - time)
Advertisement
Add Comment
Please, Sign In to add comment