Hello,
I'm trying to make a powershell function to read a .gz file and turn it into a string. I want to avoid being required to write the unzipped file to disk and so I'm trying to use a system.io.memorystream object to hold the decompressed file. Things seem to be going fairly well but when I actually go to convert my byte array into an ascii, or UTF-8, string the text isn't formatted appropriately. Let me post some code before this get's too abstract:
$file = New-Object System.IO.FileStream "C:\Temp\Entries.log.gz", ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
$type = [System.IO.Compression.CompressionMode]::Decompress
$stream = new-object -TypeName System.IO.MemoryStream
$GZipStream = New-object -TypeName System.IO.Compression.GZipStream -ArgumentList $file, $type
$buffer = New-Object byte[](1024)
$count = 0
do
{
$count = $gzipstream.Read($buffer, 0, 1024)
if ($count -gt 0)
{
$Stream.Write($buffer, 0, $count)
}
}
While ($count -gt 0)
$array = $stream.ToArray()
$GZipStream.Close()
$stream.Close()
$file.Close()After running the above code I get $array filled with a Byte[] array. in oreder to convert the byte array to a string I do the following:
$enc = [System.Text.Encoding]::ASCII $enc.GetString($array)
This seems to be close to what I want as the text then becomes readable, unfortunately the formatting is off. It seems to be adding an extra space between every character:
1 1 : 3 3 : 3 5 . 6 6 0 T r c 2 4 3 0 8 M e s s a g e instead of: 11:33:35.660 Trc 24308 MessageI figure I've missed something important but I'm not sure what. Any help wold be appreciated!
Hope that helps! Jason