Compressing data to Gzip

This topic contains 3 replies, has 3 voices, and was last updated by  sahar 2 months, 2 weeks ago.

  • Author
    Posts
  • #72062

    sahar
    Participant

    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 {
      [cmdletBinding()]
      param(
        [parameter(Mandatory = $true, ValueFromPipeline = $false)]
        [byte[]]$Data
      )
    	
      Process {
        $output = [System.IO.MemoryStream]::new()
        $gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Compress)
    		
          $gzipStream.Write($Data, 0, $Data.Length)
          $gzipStream.Close()
        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
    {
      Param(
        [string]$evidence_type,
        [string]$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

    Don Jones
    Keymaster

    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

    Chris Bakker
    Participant
  • #72184

    sahar
    Participant

    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
    

You must be logged in to reply to this topic.