Last active
February 7, 2024 03:44
-
-
Save waf/19efd0feb648d659899ff9183d85dcaf to your computer and use it in GitHub Desktop.
Get gzip/deflate-compressed content from redis in powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# one time setup | |
Install-Module PsRedis | |
# connect | |
Import-Module PsRedis | |
Connect-Redis -ConnectionString "my-redis-server.example.com" | |
# create function, given a redis key, get the bytes, ungzip/deflate them and return the string. | |
function Get-RedisKeyAndUncompress($key) { | |
# get ambient connection | |
$db = $Global:PsRedisCacheConnection.GetDatabase($Global:PsRedisDatabaseIndex) | |
# read bytes into memory stream | |
$bytes = [Byte[]]$db.StringGet($key) | |
$mem = New-Object -TypeName System.IO.MemoryStream(,$bytes) | |
# decompress | |
$deflate = New-Object System.IO.Compression.DeflateStream(@($mem, [System.IO.Compression.CompressionMode]::Decompress)) | |
$decompressed = New-Object System.IO.MemoryStream | |
$deflate.CopyTo($decompressed) | |
$deflate.Dispose() | |
# encode to string | |
return [System.Text.Encoding]::UTF8.GetString(@($decompressed.ToArray())) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To inflate from python side something similar to this is required: