Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert file to Gzip memory stream, then convert that to Base64 before exporting to text file (or var)

How do I change the below so i can use the Gzip data with [System.Convert]::ToBase64String() instead of an outfile then send THAT output(the b64) to an outfile

**I Received these answers from help on a previous post from an awesome member. It was marked as an answer but my goals were misunderstood.

Right now this does INFILE>Compress>OUTFILE, I want to do INFILE>Compress>Convert>OUTBase64

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$infile = Get-Item path\to\myfiletocompress.ext
$instream = $infile.OpenRead()
$outstream = (New-Item path\to\mygzfile.gz).OpenWrite()
$gzip = [System.IO.Compression.GZipStream]::new(
    $outstream,
    [System.IO.Compression.CompressionLevel]::Optimal)
$instream.CopyTo($gzip)

$gzip.Dispose()
$outstream.Dispose()
$instream.Dispose()

And with this the reverse, I want to use Base64 for the input from a Var, and have that get converted from base64 first then get decompressed into an outfile.

This is currently INFILE>Decompress>OUTFILE I would like to INBase64>Convert>Decompress>OUTFILE

$infile = Get-Item path\to\mygzfile.gz
$instream = $infile.OpenRead()
$outstream = (New-Item path\to\myexpandedgzfile.ext -Force).OpenWrite()
$gzip = [System.IO.Compression.GZipStream]::new(
    $instream,
    [System.IO.Compression.CompressionMode]::Decompress)
$gzip.CopyTo($outstream)

$gzip.Dispose()
$outstream.Dispose()
$instream.Dispose()

The way I’m doing it now is with no compression, using IO.File ReadAllBytes, WriteAllBytes.

File>B64

[Convert]::ToBase64String([IO.File]::ReadAllBytes('anyfile.ext')) | Out-File 'anyfile.txt"

B64>File

[IO.File]::WriteAllBytes('anyfile.ext', [Convert]::FromBase64String('anyfile.txt'))

I tested the results of using gzip files instead of originals when encoding to B64 and the B64 encoded files were smaller than the originals, so this would be worth it.

>Solution :

You need to use a MemoryStream instead of a FileStream:

# `$content` could also be a hardcoded string here
$content = Get-Content path\to\file.txt -Raw
$outstream = [System.IO.MemoryStream]::new()
$gzip = [System.IO.Compression.GZipStream]::new(
    $outstream,
    [System.IO.Compression.CompressionLevel]::Optimal)
$bytes = [System.Text.Encoding]::UTF8.GetBytes($content)
$gzip.Write($bytes, 0, $bytes.Length)
$gzip.Dispose()

# This is the b64 gzip string
[System.Convert]::ToBase64String($outstream.ToArray())
$outstream.Dispose()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading