Expand-Zippy.ps1

function Expand-Zippy
{
    <#
    .SYNOPSIS
        Expands Compressed Data
    .DESCRIPTION
        Expands Compressed Data using the .NET GZipStream class
    .LINK
        Compress-Zippy
    .Example
        Compress-Zippy -String ("abc" * 1kb) |
            Expand-Zippy
    .EXAMPLE
        Compress-Zippy "Hello World" | Expand-Zippy
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Deflate |
            Expand-Zippy -Algorithm Deflate
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm GZip |
            Expand-Zippy -Algorithm GZip
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Zlib |
            Expand-Zippy -Algorithm Zlib
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Brotli -AsByteStream |
            Expand-Zippy
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Deflate -AsByteStream |
            Expand-Zippy -Algorithm Deflate
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm GZip -AsByteStream |
            Expand-Zippy -Algorithm GZip
    .EXAMPLE
        Compress-Zippy "Hello World" -Algorithm Zlib -AsByteStream |
            Expand-Zippy -Algorithm Zlib
    #>

    
    [OutputType([string],[byte])]
    [CmdletBinding(DefaultParameterSetName='BinaryData')]
    param(    
    # The compressed data, as a Base64 string
    [Parameter(
        ValueFromPipelineByPropertyName,
        ValueFromPipeline,
        Position=0,
        ParameterSetName='CompressedData'
    )]
    [string]
    $CompressedData,
    
    # The compressed data, as a byte array
    [Parameter(
        ValueFromPipelineByPropertyName,
        ValueFromPipeline,
        Position=0,
        ParameterSetName='BinaryData'
    )]
    [Byte[]]
    $BinaryData,

    # If set, will output a `byte[]` of decompressed bytes.
    [Alias('AsBytes','AsByte')]
    [switch]
    $AsByteStream,

    # If set, will output the decompressed data as a base64 string
    [Alias('AsBase64String')]
    [switch]
    $AsBase64,

    # 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
    )   
      

    begin {
        $allInput = @()
        filter expand {
            $ms = [IO.MemoryStream]::new($BinaryData)
            $cs = New-Object "IO.Compression.${Algorithm}Stream"(
                $ms, [IO.Compression.CompressionMode]"Decompress"
            )
            #endregion Open Data

            $decompressed = [IO.MemoryStream]::new()
            $cs.CopyTo($decompressed)
            $null = $decompressed.Seek(0, 'begin')

            #region Compress And Render
            if ($AsByteStream) {
                $psCmdlet.WriteObject($decompressed.ToArray())
            }
            elseif ($AsBase64) {
                [Convert]::ToBase64String($decompressed.ToArray())
            }
            else {
                $sr = New-Object IO.StreamReader($decompressed, $Encoding)
                $strOut = $sr.ReadToEnd()
                $strOut
            }  
        }
        $allInput = @()        
    }

    
    process {
        if ($PSBoundParameters['CompressedData']) {
            try {
                $binaryData = [Convert]::FromBase64String($CompressedData)
                expand 
            } catch {
                Write-Verbose "Unable to expand base 64 string: $_"
                return
            }            
        } else {
            $allInput += ,$BinaryData
        }
    }

    end {
        foreach ($in in $allInput) {
            $BinaryData = $in
            expand
        }
        
        return         
    }    
}