# Begin ----------------------------------------------------------------

# Sub UnpackRAR
Function UnpackRAR {

  Param(
    [String]$RARFileName,
    [String]$DestPath
  )

$Unrar = @"
  $(Get-Content $PSScriptRoot\unrar.cs -Raw)
"@

  If([IntPtr]::Size -eq 8) {
    $Unrar = "#define x64" + [Environment]::NewLine + $Unrar;
  }

  [IO.Directory]::SetCurrentDirectory($($PSScriptRoot));

  Add-Type -TypeDefinition $Unrar -Language CSharp;

  $ArcHeader = New-Object Unrar+RARHeaderDataEx;

  $ArcData = New-Object Unrar+RAROpenArchiveDataEx;
  $ArcData.ArcName = $RARFileName;
  $ArcData.OpenMode = [Unrar]::RAR_OM_EXTRACT;
  $ArcData.CmtBuf = [String]::new([Char]" ", 65535);
  $ArcData.CmtBufSize = 65535;

  $hArc = [Unrar]::RAROpenArchiveEx([Ref]$ArcData);
  If(($hArc -eq $null) -or ($ArcData.OpenResult -ne [Unrar]::ERAR_SUCCESS)) {
    Return;
  }

  If($ArcData.CmtSize -gt 0) {
    Write-Host "Comment in archive file: " $ArcData.CmtBuf;
  }

  While([Unrar]::RARReadHeaderEx($hArc, [Ref]$ArcHeader) -eq [Unrar]::ERAR_SUCCESS) {
    [Void][Unrar]::RARProcessFile($hArc, [Unrar]::RAR_EXTRACT, `
      $DestPath, $null);
  }

  [Void][Unrar]::RARCloseArchive($hArc);

}

# Sub Main
Function Main {

  UnpackRAR -RARFileName "$($PSScriptRoot)\\Test.rar" `
    -DestPath "C:\\Users\\Public\\Dummy";

}

# Main
Main;

# End ------------------------------------------------------------------