

Powershell: Compress and decompress byte array
source link: https://gist.github.com/jesusninoc/b27a8294df2036de6814cd086c0ae35e
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Compress and decompress byte array · GitHub
Instantly share code, notes, and snippets.
# 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 )
Recommend
-
17
Convert between byte array/slice and string yourbasic.org/golang Basics When you convert between a stri...
-
19
Uncompress Simple library to decompress files .zip, .rar, .cbz, .cbr in React Native ...
-
7
September 28, 2021 Compress Strings With .NET and C# ...
-
13
Year End Decompress: 2020 You look at where you’re going and where you are and it never makes sense, but then you look back at where you’ve been and a pattern seems to emerge. I just love this quote from the b...
-
8
6 Ways to Compress Image Files in Windows 11 and 10 By Jack Slater Published 11 hours ago Compressing image files is a g...
-
9
Year End Decompress: 2020
-
5
Java Program to Convert Byte Array to StringSkip to content
-
10
How to Convert Stream to Byte Array in C# Posted by Januarius Njoku | Updated Date May 22, 2023...
-
9
How do you decompress after a long day of being a Maker?Vanessa Franz1d ago44 repliesI'll go first. I workout,...
-
6
Converting a Byte Array to Hexadecimal String in C#
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK