0

Trying to get GzipStream going in Posh. Have this:

[System.IO.MemoryStream] $output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Optimal)

Getting error on the New-Object call:

New-Object : Cannot find an overload for "GZipStream" and the argument count: "2".

But the docs for the class show two constructors that take two arguments.
GZipStream Constructors

What am I missing?

user1443098
  • 6,487
  • 5
  • 38
  • 67

1 Answers1

1

"Optimal" is a member of the CompressionLevel enum, not the CompressionMode enum, so your code should be:

[System.IO.MemoryStream] $output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.GzipStream($output, [IO.Compression.CompressionLevel]::Optimal)

In your particular case, PowerShell is trying to be helpful and converts non-existent enum member names to $null:

PS> $null -eq [IO.Compression.CompressionMode]::Optimal
True

PS> $null -eq [IO.Compression.CompressionMode]::NonExistentMember
True

so your original code is equivalent to:

[System.IO.MemoryStream] $output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.GzipStream $output, $null

which gives the same exception as you got. The error isn't so much that it can't find a constructor overload with 2 parameters, it's more that it found multiple suitable overloads and couldn't choose which one to use (see the first two constructors in GZipStream Constructors).

Note also that I had to wrap the constructor parameters in round brackets - this changes the way PowerShell resolves which constructor to call somehow. I'm not sure of the exact details, but using the round brackets makes it work!

[System.IO.MemoryStream] $output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.GzipStream($output, [IO.Compression.CompressionLevel]::Optimal)

Hope this helps.

mclayton
  • 8,025
  • 2
  • 21
  • 26