Compress-Zippy.ps1

function Compress-Zippy {
    <#
    .SYNOPSIS
        Quickly Compresses Content
    .DESCRIPTION
        Compresses Content using a number of standard algorithms.

        * Brotli (default)
        * Default
        * Gzip
        * Zlib
    .EXAMPLE
        Compress-Zippy "Hello World"
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Deflate
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm GZip
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Zlib
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Brotli -AsByteStream
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Deflate -AsByteStream
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm GZip -AsByteStream
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Zlib -AsByteStream
    #>

    param(
    # The text to compress
    [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Text',Position=0)]
    [Alias('String')]
    [string]
    $Text,

    # A range of bytes to compress.
    [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Bytes')]
    [Alias('byte')]
    [byte[]]
    $Bytes,

    # A stream to compress.
    [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Stream')]
    [Alias('streams')]
    [IO.Stream]
    $Stream,

    # The compression algorithm.
    # By default, Brotli.
    [ValidateSet('Brotli','Deflate', 'GZip', 'ZLib')]
    [Alias('CompressionAlgorithm')]
    [string]
    $Algorithm = 'Brotli',

    # The text encoding.
    # By default, this will be the `$outputEncoding`
    [Text.Encoding]
    $Encoding = $OutputEncoding,

    # The compression level.
    # By default, this will be `fastest`.
    [IO.Compression.CompressionLevel]
    $CompressionLevel = 'Fastest',
    
    # If set, will output a byte stream.
    # If not set, will output a base64 encoded string.
    [Alias('AsBytes','AsByte')]
    [switch]
    $AsByteStream
    )


    try {
        $compressor = "IO.Compression.${Algorithm}Stream" -as [type]

        $outputStream = [IO.MemoryStream]::new()
        $compressedStream = $compressor::new($outputStream, $CompressionLevel)
        switch ($PSCmdlet.ParameterSetName) {
            Text {
                $Bytes = $Encoding.GetBytes($Text)
                $compressedStream.Write($Bytes,0,$Bytes.Length)
            }
            Bytes {
                $compressedStream.Write($Bytes,0,$Bytes.Length)
            }
            Stream {
                $Stream.CopyTo($compressedStream)
            }
        }
        $compressedStream.Close()
        $compressedStream.Dispose()
        
        if ($AsByteStream) {
            $psCmdlet.WriteObject($outputStream.ToArray())
        }         
        else {
            [Convert]::ToBase64String(
                $outputStream.ToArray()
            )
        }                       
    } catch {
        $PSCmdlet.WriteError($_)
    }
    finally {
        if ($outputStream) {
            $outputStream.Close()
            $outputStream.Dispose()
        }    
    }    
}