- Add the following using statements to the top of your Program.cs file:
using System.IO; using System.IO.Compression;
- 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); ...