Snusmumriken

lua win 7zip binding

Oct 7th, 2019
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. local M = {}
  2. M.root = 'C:\\Program Files\\7-Zip\\7z.exe'
  3.  
  4. local function fmt(s)
  5.     if s:find(' ') and s:sub(1, 1) ~= '"' then
  6.         return '"' .. s .. '"'
  7.     end
  8.     return s
  9. end
  10.  
  11. function M.command(...)
  12.     local data = 'CALL'
  13.  
  14.     for i = 1, select('#', ...) do
  15.         data = data .. ' ' .. fmt(select(i, ...))
  16.     end
  17.     print('COMMAND [' .. data .. ']')
  18.     return data
  19. end
  20.  
  21. local File = {}
  22. File.__index = File
  23.  
  24. function File:new(o)
  25.     return setmetatable(o or {}, self)
  26. end
  27.  
  28. function File:extract(dst)
  29.     return os.execute(M.command(M.root, "x", "-y", self.src, self.path, '-o' .. dst))
  30. end
  31.  
  32. function File:read(dst)
  33.     if self.data then return self.data end
  34.    
  35.     local f = io.popen(M.command(M.root, 'x', '-so', self.src, self.path), 'rb')
  36.     self.data = f:read("*a")
  37.     f:close()
  38.    
  39.     return self.data
  40. end
  41.  
  42. function M.list(src, filter)
  43.     local files = {}
  44.    
  45.     local command = M.command(M.root, 'l', src, filter or '*')
  46.     local listing = io.popen(command, 'rb')
  47.    
  48.     local nextLine = listing:lines()
  49.    
  50.     local line = nextLine()
  51.     while line:sub(1, 2) ~= '--' do
  52.         line = nextLine()
  53.     end
  54.     line = nextLine()
  55.  
  56.     while line:sub(1, 2) ~= '--' do
  57.         local key, value = line:match('(.-) = (.*)')
  58.         if key and value then
  59.             files[key:lower():gsub('%W', '_')] = tonumber(value) or value
  60.         end
  61.         line = nextLine()
  62.     end
  63.    
  64.     line = nextLine()
  65.  
  66.     while line:sub(1, 2) ~= '--' do
  67.         local o = {}
  68.         o.src = src
  69.         o.date, o.time, o.res, o.size, o.compressed, o.path = line:match('(.-)%s+(.-)%s+(.-)%s+(.-)%s+(.-)%s+(.*)')
  70.         if o.date then
  71.             table.insert(files, File:new(o))
  72.         end
  73.         line = nextLine()
  74.     end
  75.    
  76.     listing:close()
  77.     return files
  78. end
  79.  
  80. function M.extract(src, dst, filter)
  81.     filter = filter or '*'
  82.     return os.execute(M.command(M.root, "x", "-y", src, filter, '-o' .. dst))
  83. end
  84.  
  85. function M.read(src, filter)
  86.     filter = filter or '*'
  87.    
  88.     local list = M.list(src, filter)
  89.    
  90.     local file = io.popen(M.command(M.root, 'x', '-so', src, filter), 'rb')
  91.     local data = file:read("*a"); file:close()
  92.    
  93.     local pos = 1
  94.     for i, v in ipairs(list) do
  95.         --print(pos, v.size)
  96.         local filedata = data:sub(pos, pos + v.size - 1)
  97.         pos = pos + v.size
  98.         v.data = filedata
  99.     end
  100.    
  101.     return list
  102. end
  103.  
  104. if not ... then return M end
  105.  
  106. function lines_iter(s, pos, d)
  107.     print(s, pos, d)
  108.     pos = pos + 1
  109.     if pos > #s then return nil end
  110.    
  111.     local i, j = s:find('\n', pos)
  112.    
  113.     if not j then
  114.         return #s, s:sub(pos)
  115.     end
  116.    
  117.     local v = s:sub(pos, j - 1)
  118.     return j, v
  119. end
  120.  
  121. function string.lines(s)
  122.     return lines_iter, s, 0
  123. end
  124.  
  125.  
  126.  
  127. local time = os.clock()
  128.  
  129. local files = M.read('C:\\7-Zip\\processed.zip', '*')
  130.  
  131. local file = files[1004]
  132. print(file.data)
  133.  
  134. print("Len", len, "Time", os.clock() - time)
Advertisement
Add Comment
Please, Sign In to add comment