I hope this comes in handy for someone, I'm still working on it hopefully more features to come yet. But heres the basic function.
Code:
ExtractRAR(File,ExPath = "",ExPaths = 1,Pass = ""){
If (ExPath = ""){
ExPath:=A_workingdir
}
varsetcapacity(ArchiveData,32,0)
Numput(&File,ArchiveData)
Numput(1,ArchiveData,4)
hModule := DllCall("LoadLibrary", "str", "unrar.dll")
if (!hModule) {
Msgbox,Could not Load Library
Return
}
hArchive:=DLLCall("unrar.dll\RAROpenArchive","Uint",&ArchiveData)
if (password != ""){
DLLCall("unrar.dll\RARSetPassword","UInt",hArchive,"uint",&password)
}
Varsetcapacity(ArchiveHeader,572,0)
Loop
{
FileResult:=DLLCall("unrar.dll\RARReadHeader","UInt",hArchive,"Uint",&ArchiveHeader)
If (FileResult = 12){
MsgBox,File Header Currupt - Incorrect Password????
Return
}
Else If (FileResult = 10){
Break
}
Varsetcapacity(CurFile,260,0)
DllCall("lstrcpynA", "Str", CurFile, "UInt", &ArchiveHeader+260, "int", 260)
If (substr(ExPath,0,1) != "\"){
Expath.="\"
}
If (ExPaths){
File:=ExPath . CurFile
}
Else {
File:=ExPath . Regexreplace(CurFile,".*\\","")
}
ExResult:=DLLCall("unrar.dll\RARProcessFile","UInt",hArchive,"Uint",2,"Uint",0,"Uint",&File)
If (ExResult != 0){
MsgBox,Error Extracting File
Return
}
}
DLLCall("unrar.dll\RARCloseArchive","Uint",hArchive)
DllCall("FreeLibrary ", "UInt", hModule)
Return
}
It requires Unrar.dll unrar.dll.
Just the DLL or
The full packageUsage Is ExtractRAR(RarFile,[ExtractionPath,ExtractPaths,Password])
Define Rarfile as the file you wish to extract.
Extraction Path where you want to extract to. (Leave Blank to default to working Dir)
ExtractPaths - Set to 1 if you wish to extract full paths from the Archive, 0 if not. (Leave Blank to Extract Paths)
Password, Enter a password if the Rar requires it.
The only Required parameter is Filename, but as with all functions if you want to define one of the later two fields, you have to define all fields before it.
Version 0.90Code:
ExtractRAR(File,ExPath = "",ExPaths = 1,ShowProg = 0){
Global Prog,UnpackedFS
If (ExPath = ""){
ExPath:=A_workingdir
}
Fname:=File
varsetcapacity(ArchiveData,32,0)
Numput(&File,ArchiveData)
Numput(1,ArchiveData,4)
hModule := DllCall("LoadLibrary", "str", "unrar.dll")
if (!hModule) {
MsgBox , 262160 , %A_ScriptName% ,Could not Load Library
Return
}
hArchive:=DLLCall("unrar.dll\RAROpenArchive","Uint",&ArchiveData)
Result:=Numget(&ArchiveData,8)
If (Result = 11){
MsgBox , 262160 , %A_ScriptName% ,Not enough Memory
Return
}
Else if (Result = 12){
MsgBox , 262160 , %A_ScriptName% ,Archive Header Broken
Return
}
Else If (Result = 13){
MsgBox , 262160 , %A_ScriptName% ,Invalid Archive Type
Return
}
Else IF (Result = 14){
MsgBox , 262160 , %A_ScriptName% ,Unknown encryption
Return
}
Else If (Result = 15){
MsgBox , 262160 , %A_ScriptName% ,Error Opening File
Return
}
If (ShowProg){
Address:=registercallback("RarCallback","",4,1)
If (Address = ""){
Msgbox , 262160 , %A_ScriptName% , Error extracting archive!`n`nReturned adress to machine-code function was empty.
}
}
Else {
Address:=registercallback("RarCallback","",4,0)
If (Address = ""){
Msgbox , 262160 , %A_ScriptName% , Error extracting archive!`n`nReturned adress to machine-code function was empty.
}
}
DLLCall("unrar.dll\RARSetCallback","UInt",hArchive,"Uint",Address,"Uint",0)
Varsetcapacity(ArchiveHeader,572,0)
Loop
{
FileResult:=DLLCall("unrar.dll\RARReadHeader","UInt",hArchive,"Uint",&ArchiveHeader)
If (FileResult = 12){
MsgBox , 262160 , %A_ScriptName% ,File Header Currupt - Incorrect Password????
Return
}
Else If (FileResult = 10){
Break
}
Varsetcapacity(CurFile,260,0)
DllCall("lstrcpynA", "Str", CurFile, "UInt", &ArchiveHeader+260, "int", 260)
UnpackedFS:=Numget(&ArchiveHeader,528)
If (ShowProg){
Progress,A R0-%UnpackedFS% M P0 w500,Current File %CurFile%,Extracting From %Fname%,Extraction in progress...
Prog:=0
Sleep,100
}
If (substr(ExPath,0,1) != "\"){
Expath.="\"
}
If (ExPaths){
File:=ExPath . CurFile
}
Else {
File:=ExPath . Regexreplace(CurFile,".*\\","")
}
ExResult:=DLLCall("unrar.dll\RARProcessFile","UInt",hArchive,"Uint",2,"Uint",0,"Uint",&File)
If (ExResult != 0){
MsgBox , 262160 , %A_ScriptName% ,Error Extracting File
Return
}
}
Progress,Off
DLLCall("unrar.dll\RARCloseArchive","Uint",hArchive)
DllCall("FreeLibrary ", "UInt", hModule)
Return
}
RarCallback(Message,User,P1,P2){
Global Prog
If (Message = 1 and a_eventinfo = 1){
Prog+=P2
Progress,%Prog%
}
If (Message = 2){
Inputbox,password
DllCall("lstrcpynA","Uint",P1+0, "STR",Password,"int", P2)
}
return,1
}
The updated version 0.85 has a slightly different calling method.
Usage Is ExtractRAR(RarFile,[ExtractionPath,ExtractPaths,Showprogress])
The main difference in calling is password has been replace with the option to show a progress bar. If a password is needed for the file you will be prompted for one.
As usual, Comments, Suggestions, Questions etc, welcome.