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 |
Function ConvertTo-GZipString () { <# .SYNOPSIS Compresses a string with the GZip algorithm .DESCRIPTION Compresses a string with the GZip algorithm and returns the result in a Base64 string .PARAMETER String Any plain text string to be compressed .EXAMPLE dir | Out-String | ConvertTo-GZipString .LINK ConvertFrom-GZipString #> [CmdletBinding()] Param( [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)] $String ) Process { $String | ForEach-Object { $ms = New-Object System.IO.MemoryStream $cs = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Compress) $sw = New-Object System.IO.StreamWriter($cs) $sw.Write($_) $sw.Close() [System.Convert]::ToBase64String($ms.ToArray()) } } } Function ConvertFrom-GZipString () { <# .SYNOPSIS Decompresses a Base64 GZipped string .DESCRIPTION Decompresses a Base64 GZipped string .PARAMETER String A Base64 encoded GZipped string .EXAMPLE $compressedString | ConvertFrom-GZipString .LINK ConvertTo-GZipString #> [CmdletBinding()] Param( [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)] $String ) Process { $String | ForEach-Object { $compressedBytes = [System.Convert]::FromBase64String($_) $ms = New-Object System.IO.MemoryStream $ms.write($compressedBytes, 0, $compressedBytes.Length) $ms.Seek(0,0) | Out-Null $cs = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress) $sr = New-Object System.IO.StreamReader($cs) $sr.ReadToEnd() } } } |