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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.