Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Powershell: Compress and decompress byte array
# Compress and decompress byte array
function Get-CompressedByteArray {
[CmdletBinding()]
Param (
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[byte[]] $byteArray = $(Throw("-byteArray is required"))
)
Process {
Write-Verbose "Get-CompressedByteArray"
[System.IO.MemoryStream] $output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Compress)
$gzipStream.Write( $byteArray, 0, $byteArray.Length )
$gzipStream.Close()
$output.Close()
$tmp = $output.ToArray()
Write-Output $tmp
}
}
function Get-DecompressedByteArray {
[CmdletBinding()]
Param (
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[byte[]] $byteArray = $(Throw("-byteArray is required"))
)
Process {
Write-Verbose "Get-DecompressedByteArray"
$input = New-Object System.IO.MemoryStream( , $byteArray )
$output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)
$gzipStream.CopyTo( $output )
$gzipStream.Close()
$input.Close()
[byte[]] $byteOutArray = $output.ToArray()
Write-Output $byteOutArray
}
}
[string] $text = "some text to encode"
Write-Host "Text: " ( $text | Out-String )
[System.Text.Encoding] $enc = [System.Text.Encoding]::UTF8
[byte[]] $encText = $enc.GetBytes( $text )
$compressedByteArray = Get-CompressedByteArray -byteArray $encText
Write-Host "Encoded: " ( $enc.GetString( $compressedByteArray ) | Out-String )
$decompressedByteArray = Get-DecompressedByteArray -byteArray $compressedByteArray
Write-Host "Decoded: " ( $enc.GetString( $decompressedByteArray ) | Out-String )
@helmutsreinis

This comment has been minimized.

Copy link

helmutsreinis commented Dec 12, 2017

Is there any way to make this script compress data to file and make $decompressedByteArray read it from that file instead of variable?

@JaeTLDR

This comment has been minimized.

Copy link

JaeTLDR commented Apr 9, 2018

@helmutsreinis have you tried $(get-content c:\path\to\file into the parameter)

@gbritton1

This comment has been minimized.

Copy link

gbritton1 commented Aug 30, 2019

Running this today I get:

New-Object : Cannot find an overload for "GZipStream" and the argument count: "2".
At line:11 char:23
+ ... zipStream = New-Object System.IO.Compression.GzipStream $output, ([IO ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Though I actually see two overloads in the docs. How to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.