File: Expand-ArchiveExt.ps1

package info (click to toggle)
obs-3d-effect 0.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 532 kB
  • sloc: ansic: 345; makefile: 23; cpp: 16
file content (70 lines) | stat: -rwxr-xr-x 2,424 bytes parent folder | download | duplicates (40)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function Expand-ArchiveExt {
    <#
        .SYNOPSIS
            Expands archive files.
        .DESCRIPTION
            Allows extraction of zip, 7z, gz, and xz archives.
            Requires tar and 7-zip to be available on the system.
            Archives ending with .zip but created using LZMA compression are
            expanded using 7-zip as a fallback.
        .EXAMPLE
            Expand-ArchiveExt -Path <Path-To-Your-Archive>
            Expand-ArchiveExt -Path <Path-To-Your-Archive> -DestinationPath <Expansion-Path>
    #>

    param(
        [Parameter(Mandatory)]
        [string] $Path,
        [string] $DestinationPath = [System.IO.Path]::GetFileNameWithoutExtension($Path),
        [switch] $Force
    )

    switch ( [System.IO.Path]::GetExtension($Path) ) {
        .zip {
            try {
                Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force:$Force
            } catch {
                if ( Get-Command 7z ) {
                    Invoke-External 7z x -y $Path "-o${DestinationPath}"
                } else {
                    throw "Fallback utility 7-zip not found. Please install 7-zip first."
                }
            }
            break
        }
        { ( $_ -eq ".7z" ) -or ( $_ -eq ".exe" ) } {
            if ( Get-Command 7z ) {
                Invoke-External 7z x -y $Path "-o${DestinationPath}"
            } else {
                throw "Extraction utility 7-zip not found. Please install 7-zip first."
            }
            break
        }
        .gz {
            try {
                Invoke-External tar -x -o $DestinationPath -f $Path
            } catch {
                if ( Get-Command 7z ) {
                    Invoke-External 7z x -y $Path "-o${DestinationPath}"
                } else {
                    throw "Fallback utility 7-zip not found. Please install 7-zip first."
                }
            }
            break
        }
        .xz {
            try {
                Invoke-External tar -x -o $DestinationPath -f $Path
            } catch {
                if ( Get-Command 7z ) {
                    Invoke-External 7z x -y $Path "-o${DestinationPath}"
                } else {
                    throw "Fallback utility 7-zip not found. Please install 7-zip first."
                }
            }
        }
        default {
            throw "Unsupported archive extension provided."
        }
    }
}