Compressing data to Gzip

Welcome Forums General PowerShell Q&A Compressing data to Gzip

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 9 months ago.

  • Author
  • #72062

    Points: 0
    Rank: Member

    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
  • #72104

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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 ;).

  • #72113

    Points: 1
    Rank: Member
  • #72184

    Points: 0
    Rank: Member

    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

The topic ‘Compressing data to Gzip’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort