Expand-ArchiveExt.ps1 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. function Expand-ArchiveExt {
  2. <#
  3. .SYNOPSIS
  4. Expands archive files.
  5. .DESCRIPTION
  6. Allows extraction of zip, 7z, gz, and xz archives.
  7. Requires tar and 7-zip to be available on the system.
  8. Archives ending with .zip but created using LZMA compression are
  9. expanded using 7-zip as a fallback.
  10. .EXAMPLE
  11. Expand-ArchiveExt -Path <Path-To-Your-Archive>
  12. Expand-ArchiveExt -Path <Path-To-Your-Archive> -DestinationPath <Expansion-Path>
  13. #>
  14. param(
  15. [Parameter(Mandatory)]
  16. [string] $Path,
  17. [string] $DestinationPath = [System.IO.Path]::GetFileNameWithoutExtension($Path),
  18. [switch] $Force
  19. )
  20. switch ( [System.IO.Path]::GetExtension($Path) ) {
  21. .zip {
  22. try {
  23. Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force:$Force
  24. } catch {
  25. if ( Get-Command 7z ) {
  26. Invoke-External 7z x -y $Path "-o${DestinationPath}"
  27. } else {
  28. throw "Fallback utility 7-zip not found. Please install 7-zip first."
  29. }
  30. }
  31. break
  32. }
  33. { ( $_ -eq ".7z" ) -or ( $_ -eq ".exe" ) } {
  34. if ( Get-Command 7z ) {
  35. Invoke-External 7z x -y $Path "-o${DestinationPath}"
  36. } else {
  37. throw "Extraction utility 7-zip not found. Please install 7-zip first."
  38. }
  39. break
  40. }
  41. .gz {
  42. try {
  43. Invoke-External tar -x -o $DestinationPath -f $Path
  44. } catch {
  45. if ( Get-Command 7z ) {
  46. Invoke-External 7z x -y $Path "-o${DestinationPath}"
  47. } else {
  48. throw "Fallback utility 7-zip not found. Please install 7-zip first."
  49. }
  50. }
  51. break
  52. }
  53. .xz {
  54. try {
  55. Invoke-External tar -x -o $DestinationPath -f $Path
  56. } catch {
  57. if ( Get-Command 7z ) {
  58. Invoke-External 7z x -y $Path "-o${DestinationPath}"
  59. } else {
  60. throw "Fallback utility 7-zip not found. Please install 7-zip first."
  61. }
  62. }
  63. }
  64. default {
  65. throw "Unsupported archive extension provided."
  66. }
  67. }
  68. }