Functions/Compress-7Zip.ps1

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<#
.SYNOPSIS
Create a compressed archive of a file or folder
.DESCRIPTION
Use Compress-7Zip to create a 7z, gzip, zip, bzip2 or tar archive.
.EXAMPLE
Compress-7Zip c:\scripts

Create archive.zip in the current working folder of the folder c:\scripts

.EXAMPLE
Compress-7Zip "computer inventory.csv" -OutputFile "inventory.gz" -ArchiveType GZIP -Remove $True

Create a gzip archive of a single file and delete the uncompressed file

.EXAMPLE
Get-ChildItem E:\test | Where {$_.LastWriteTime -lt (Get-Date).AddDays(-5)} | Select -First 1 | Compress-7Zip

Create an archive in c:\folder based on pipeline input

.PARAMETER FullName
The full path of the file or folder you would like turn into a compressed archive.
.PARAMETER OutputFile
The full path of the file to be created. Defaults to archive.zip in current working directory.
.PARAMETER ArchiveType
The type of archive you would like. Valid types 7Z, GZIP, ZIP, BZIP2, TAR.
.PARAMETER Remove
If $True this will remove the uncompressed version of the file or folder only leaving the compressed archive.
.LINK
http://gavineke.com/PS7Zip/Compress-7Zip
#>

Function Compress-7Zip {
    [CmdletBinding(HelpUri='http://gavineke.com/PS7Zip/Compress-7Zip')]
    Param(
        [Parameter(Mandatory=$True,Position=0,ValueFromPipelineByPropertyName=$True)]
        [ValidateScript({Test-Path $_})]
        [string]$FullName,

        [Parameter()]
        [string]$OutputFile,

        [Parameter()]
        [ValidateSet("7Z","GZIP","ZIP","BZIP2","TAR")]
        [string]$ArchiveType,

        [Parameter()]
        [switch]$Remove
    )
    
    Begin {
        If($PSBoundParameters.ContainsKey('ArchiveType')) {
            If ($ArchiveType -eq '7Z') {
                $7zaArchiveType = '-t7z'
                $ArchiveExt = '.7z'
            } ElseIf ($ArchiveType -eq 'GZIP') {
                $7zaArchiveType = '-tgzip'
                $ArchiveExt = '.gz'
            } ElseIf ($ArchiveType -eq 'ZIP') {
                $7zaArchiveType = '-tzip'
                $ArchiveExt = '.zip'
            } ElseIf ($ArchiveType -eq 'BZIP2') {
                $7zaArchiveType = '-tbzip2'
                $ArchiveExt = '.bzip2'
            } ElseIf ($ArchiveType -eq 'TAR') {
                $7zaArchiveType = '-ttar'
                $ArchiveExt = '.tar'
            }
        } Else {
            $7zaArchiveType = '-tzip'
            $ArchiveExt = '.zip'
        }
        
        If(!($PSBoundParameters.ContainsKey('OutputFile'))) {
            $OutputFile = ".\archive$ArchiveExt"
        }
    }
    
    Process {
        Write-Verbose -Message 'Creating compressed archive file'
        & "$7zaBinary" a "$7zaArchiveType" "$OutputFile" "$FullName"
        
        If ($Remove) {
            Write-Verbose -Message 'Removing original files/folders'
            Remove-Item -Path "$FullName" -Force
        }
    }
    
    End {}
    
}