How to do it...

  1. Add the following using statements to the top of your Program.cs file:
        using System.IO;        using System.IO.Compression;
  1. Create a method called ZipIt() and add the code to it to ZIP the Documents directory. The code is pretty straightforward to understand. I want to, however, highlight the use of the CreateFromDirectory() method. Notice that we have set the compression level to CompressionLevel.Optimal and set the includeBaseDirectory parameter to false:
        private static void ZipIt(string path)        {          string sourceDirectory = $"{path}Documents";          if (Directory.Exists(sourceDirectory))          {            string archiveName = $"{path}DocumentsArchive.zip";            ZipFile.CreateFromDirectory(sourceDirectory, archiveName,  CompressionLevel.Optimal, false); ...

Get C# 7 and .NET Core Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.