deflate stream c# code example

Example: stream deflator c#

using System;
using System.IO;
using System.IO.Compression;

public class Program
{
    public static void Compress(DirectoryInfo directorySelected)
    {
      using (FileStream compressedFileStream = File.Create(file.FullName + ".cmp"))
      {
        using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionMode.Compress))
        {
          originalFileStream.CopyTo(compressionStream);
        }
      }
    }

    public static void Decompress(FileInfo fileToDecompress)
    {
      using (FileStream decompressedFileStream = File.Create(newFileName))
      {
        using (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))
        {
          decompressionStream.CopyTo(decompressedFileStream);
          Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
        }
      }
    }
}