Skip to content

Instantly share code, notes, and snippets.

@marcgeld
Last active April 11, 2023 09:11
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save marcgeld/bfacfd8d70b34fdf1db0022508b02aca to your computer and use it in GitHub Desktop.
Save marcgeld/bfacfd8d70b34fdf1db0022508b02aca to your computer and use it in GitHub Desktop.
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 )
@rolambert
Copy link

rolambert commented Jul 15, 2020

  • my issue is if the input is a compressed folder the output is a decompressed somthing, trying to wrap my head around this one
$file = [System.IO.File]::ReadAllBytes("C:\temp\webcontrol\wc.gz.zip")

$im = New-Object System.IO.MemoryStream(,$file)

$gzipStream = New-Object System.IO.Compression.GzipStream $im, ([IO.Compression.CompressionMode]::Decompress)

$output = New-Object System.IO.MemoryStream

$gzipStream.CopyTo($output)

$gzipStream.Close()

$im.Close()

$filebytes = $output.ToArray() 

  • here is my issue, my output is undefined

[io.file]::WriteAllBytes("C:\temp\webcontrol\wco",$filebytes)

@rolambert
Copy link

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?

  • this removed the overload for me
$file = [System.IO.File]::ReadAllBytes("C:\temp\webcontrol\wc.gz.zip")

$im = New-Object System.IO.MemoryStream(,$file)

@JonathonFS
Copy link

JonathonFS commented Jan 12, 2023

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?

The $input variable is special and can cause weird behavior if you try to use it like a normal variable. This particular error is being caused because the $input variable is of type ArrayListEnumeratorSimple, but the method expects a MemoryStream variable. Just find an replace all instances of $input with another variable name, like $inStream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment