CK.Core.FileUtil.CompressFileToGzipFile C# (CSharp) Method

CompressFileToGzipFile() public static method

Compresses a file to another file, using GZip at the given compression level.
public static CompressFileToGzipFile ( string sourceFilePath, string destinationPath, bool deleteSourceFileOnSuccess, CompressionLevel level = CompressionLevel.Optimal, int bufferSize = 64*1024 ) : void
sourceFilePath string The source file path.
destinationPath string The destination path. If it doesn't exist, it will be created. If it exists, it will be replaced.
deleteSourceFileOnSuccess bool if set to true, will delete source file if no errors occured during compression.
level CompressionLevel Compression level to use.
bufferSize int Size of the buffer, in bytes.
return void
        public static void CompressFileToGzipFile( string sourceFilePath, string destinationPath, bool deleteSourceFileOnSuccess, CompressionLevel level = CompressionLevel.Optimal, int bufferSize = 64*1024 )
        {
            using( FileStream source = new FileStream( sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, useAsync: false ) )
            {
                using( FileStream destination = new FileStream( destinationPath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, useAsync: false ) )
                {
                    using( GZipStream gZipStream = new GZipStream( destination, level ) )
                    {
                        source.CopyTo( gZipStream, bufferSize );
                    }
                }
            }
            if( deleteSourceFileOnSuccess )
            {
                File.Delete( sourceFilePath );
            }
        }