Edit

Share via


CompressionLevel Enum

Definition

Specifies values that indicate whether a compression operation emphasizes speed or compression size.

C#
public enum CompressionLevel
Inheritance
CompressionLevel

Fields

Name Value Description
Optimal 0

The compression operation should optimally balance compression speed and output size.

Fastest 1

The compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed.

NoCompression 2

No compression should be performed on the file.

SmallestSize 3

The compression operation should create output as small as possible, even if the operation takes a longer time to complete.

Remarks

Compression operations usually involve a tradeoff between the speed and the effectiveness of compression. You use the CompressionLevel enumeration to indicate which factor is more important in your development scenario: the time to complete the compression operation or the size of the compressed file. These values do not correspond to specific compression levels; the object that implements compression determines how to handle them.

The following methods of the DeflateStream, GZipStream, ZipArchive, ZipFile, and ZipFileExtensions classes include a parameter named compressionLevel that lets you specify the compression level:

Examples

The following example shows how to set the compression level when creating a zip archive by using the ZipFile class.

C#
using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";

            ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
        }
    }
}

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0