Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set the level of compression when using System.IO.Compression.GZipStream

Is it possible to set compression level when using .NET's GZipStream to compress a stream? It seems that Ionic Zip has a constructor for this, but I would rather not use a library just to get this feature.

like image 691
Mike Schall Avatar asked Jun 27 '11 15:06

Mike Schall


1 Answers

As of .NET 4.5, the CompressionLevel enum has been added to a couple constructors for GZipStream.

The options are:

CompressionLevel.Optimal = 0
CompressionLevel.Fastest = 1
CompressionLevel.NoCompression = 2

So, you can now create a GZipStream by passing a CompressionLevel which determines how much to compress the data.

using (GZipStream compressionStream = new GZipStream(stream, CompressionLevel.Optimal))
{
    // ...
}

More information is available in the GZipStream documentation or the CompressionLevel documentation.

like image 52
johnnyRose Avatar answered Nov 10 '22 00:11

johnnyRose