Author Posts

June 4, 2017 at 8:31 am

Hello guys,
i've been trying for a long time to compress data to gzip but i just can't seem to make it work.
This is my Gzip conversion code:

function ConvertTo-GzipData {
    [parameter(Mandatory = $true, ValueFromPipeline = $false)]
  Process {
    $output = [System.IO.MemoryStream]::new()
    $gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Compress)
      $gzipStream.Write($Data, 0, $Data.Length)
    return $output.ToArray()

And this is the function that uses the gzip compression funtion.
This function gets a string which goes to $data

function Send-Data
    $encoding = [System.Text.Encoding]::UTF8
    $enc_data = $encoding.GetBytes($data)
    $gzippedData = ConvertTo-GzipData -Data $enc_data
    write-host $gzippedData

i get the following error :

ConvertTo-GzipData : Cannot process argument transformation on parameter 'Data'. Cannot convert the "System.Byte[]" value of type 
"System.Byte[]" to type "System.Byte".
At line:10 char:45
+     $gzippedData = ConvertTo-GzipData -Data $enc_data
+                                             ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertTo-GzipData], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,ConvertTo-GzipData

June 5, 2017 at 1:36 pm

So, the error is telling you that you're feeding it a byte array, but it wants a single byte. This is a good bit beyond PowerShell, really; you're in raw .NET. I only mention it because you might find that a dev site like StackOverflow offers a better way to get a good answer on this one ;).

June 6, 2017 at 1:13 pm

Thanks guys, i think i managed to compress the data yet i encountered a different error while send it via Invoke-Webrequest
This is the command i used:

Invoke-WebRequest -Method Post -Headers @{'Content-Encoding' = 'gzip'} -Body $gzippedData -Uri $someUrl

and this is the error i recive:

Invoke-WebRequest : Failed to decompress body
At line:93 char:6
+      Invoke-WebRequest -Method Post -TransferEncoding gzip -Headers @ ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand