Function RARExtract(ByVal sRARArchive As String, ByVal sDestPath As String, Optional ByVal sPassword As String) As Integer
' Description:-
' Exrtact file(s) from RAR archive.
' Parameters:-
' sRARArchive = RAR Archive filename
' sDestPath = Destination path for extracted file(s)
' sPassword = Password [OPTIONAL]
' Returns:-
' Integer = 0 Failed (no files, incorrect PW etc)
' -1 Failed to open RAR archive
' >0 Number of files extracted
Dim lHandle As Long
Dim lStatus As Long
Dim uRAR As RAROpenArchiveData
Dim uHeader As RARHeaderData
Dim iFileCount As Integer
RARExtract = -1
' Open the RAR
uRAR.ArcName = sRARArchive
uRAR.OpenMode = RAR_OM_EXTRACT
lHandle = RAROpen(uRAR)
' Failed to open RAR ?
If uRAR.OpenResult <> 0 Then Exit Function
' Password ?
If sPassword <> "" Then
RARSetPassword lHandle, sPassword
End If
' Extract file(s)...
iFileCount = 0
' Is there at lease one archived file to extract ?
lStatus = RARReadHdr(lHandle, uHeader)
Do Until lStatus <> 0
' Process (extract) the current file within the archive
If RARProcFile(lHandle, RAR_EXTRACT, "", sDestPath + uHeader.FileName) = 0 Then
iFileCount = iFileCount + 1
End If
' Is there another archived file in this RAR ?
lStatus = RARReadHdr(lHandle, uHeader)
Loop
' Close the RAR
RARClose lHandle
' Return
RARExtract = iFileCount
End Function