O'Reilly logo

C# 7 and .NET Core Cookbook by Dirk Strauss

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required