Skip to content

Instantly share code, notes, and snippets.

@leeonix
Last active March 4, 2017 15:33
Show Gist options
  • Save leeonix/fe08ca1a7179d3f6add1 to your computer and use it in GitHub Desktop.
Save leeonix/fe08ca1a7179d3f6add1 to your computer and use it in GitHub Desktop.
Use lua to extract rar file for Total Commander
--
-- FILE: extract_winrar.lua
-- AUTHOR: LeeoNix
-- DESCRIPTION:
-- NOTES: ---
--
--[[
TOTALCMD#BAR#DATA
cmd /c lua %COMMANDER_PATH%\Plugins\extract_winrar.lua
%l %t "izzs.net"
wciconex.dll,50
用Lua脚本写的4个unrar解压一次的脚本
--]]
local lfs = require 'lfs'
local alien = require 'alien'
local w32 = require 'w32'
local kernel = alien.load 'kernel32.dll'
kernel.WinExec:types { "string", "int", abi = "stdcall" }
kernel.Sleep:types { "int", abi = "stdcall" }
local win_exec = kernel.WinExec
local sleep = kernel.Sleep
local WAIT_OBJECT_0 = 0
local WAIT_TIMEOUT = 0x102
local WAIT_FAILED = 0xFFFFFFFF
--------------------------------------------------------------------------------
local listfile = arg[1]
local targetdir = arg[2]
local password = arg[3]
local use_name_dir = arg[4]
--------------------------------------------------------------------------------
local rar = 'd:\\tools\\winrar\\rar.exe'
local rar_fmt = '"%s" -ow -p%s -y x "%s" "%s"'
local wait_count = 6
local function file_walk(func)
local i = 1
for l in io.lines(listfile) do
func(l, i)
i = i + 1
end -- end for
end -- end function
--------------------------------------------------------------------------------
local function do_win_exec()
local rar_cmd = rar .. ' -ow -p%s -y x %s %s'
file_walk(function (l, i)
local cmd = string.format(rar_cmd, password, l, targetdir)
print(cmd)
win_exec(cmd, 1)
if i % wait_count == 0 then
sleep(20 * 1000)
end -- end if
end) -- end function
end -- end function
--------------------------------------------------------------------------------
-- w32.CreateProcess(name, cmdline, lpProcessAttributes.bInheritHandle, lpThreadAttributes.bInheritHandle, bInheritHandle,
-- lpStartupInfo is a table of LPSTARTUPINFO, lpCurrentDirectory)
local function create_process(name, param, currentdir)
return w32.CreateProcess(name, param, nil, nil, 0, 0, nil, currentdir)
end -- end function
local handles = {}
local function do_create_process()
local function use_target_dir(l)
return string.format(rar_fmt, rar, password, l, targetdir)
end -- end function
local function use_target_and_name_dir(l)
local filename = string.match(l, '.*\\(.+)%.rar$')
return string.format(rar_fmt, rar, password, l, targetdir .. filename .. '\\')
end -- end function
local get_param
if use_name_dir == 'use_name' then
get_param = use_target_and_name_dir
else
get_param = use_target_dir
end -- end if
file_walk(function (l, i)
local param = get_param(l)
print(param)
local res, process = create_process(rar, param, lfs.currentdir())
if res == 1 then
table.insert(handles, process)
end -- end if
print(res, process)
if i % wait_count == 0 then
local wait_res = w32.WaitForMultipleObjects(handles, 1, 0xFFFFFFFF)
if WAIT_OBJECT_0 == wait_res then
handles = {}
end -- end if
-- w32.WaitForSingleObject(process, 0xFFFFFFFF)
end -- end if
end) -- end function
end -- end function
--do_win_exec()
do_create_process()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment