When working with local files, you might need to open, create, or update Zip files. In this article, we will learn how to work with Zip files in C#. We will learn how to perform basic operations such as opening, extracting, and creating a Zip file. The main class we will use is named and comes from the namespace. It’s been present in since .NET Framework 4.5, so we can say it’s pretty stable 😉 Nevertheless, . Let’s learn! ZipFile System.IO.Compression C# there are some tricky points that you need to know before using this class Using C# to list all items in a Zip file Once you have a Zip file, you can access the internal items without extracting the whole Zip. You can use the method. ZipFile.Open using ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Read); System.Collections.ObjectModel.ReadOnlyCollection<ZipArchiveEntry> entries = archive.Entries; Notice that I specified the . This is an Enum whose values are , , and . ZipArchiveMode Read Create Update Using the property of the , you can access the whole list of files stored within the Zip folder, each represented by a instance. Entries ZipArchive ZipArchiveEntry The object contains several fields, like the file’s name and the full path from the root archive. ZipArchiveEntry There are a few about the entries listed in the . key points to remember ZipArchiveEntry It is a : it means that even if you find a way to add or update the items in memory, the changes are not applied to the actual files; ReadOnlyCollection<ZipArchiveEntry> , not only those at the root level. As you can see from the image above, it lists both the files at the root level, like , and those in inner folders, such as ; It lists all files and folders File.txt TestZip/InnerFolder/presentation.pptx Each file is characterized by two similar but different properties: is the actual file name (like ), while contains the path from the root of the archive (e.g. ); Name presentation.pptx FullName TestZip/InnerFolder/presentation.pptx It lists : in the image above, you can see . You can recognize them because their property is empty, and their is 0; folders as if they were files TestZip/InnerFolder Name Length Lastly, remember that returns an , so you should place the operations within a statement. ZipFile.Open IDisposable using ❓❓A question for you! Why do we see an item for the folder, but there is no reference to the folder? Drop a comment below 📩 TestZip/InnerFolder TestZip Using C# to extract the Zip to a local path Extracting a Zip folder is easy but not obvious. We have only one way to do that: by calling the method. ZipFile.ExtractToDirectory It accepts as mandatory parameters the path of the Zip file to be extracted and the path to the destination: var zipPath = @"C:\Users\d.bellone\Desktop\TestZip.zip"; var destinationPath = @"C:\Users\d.bellone\Desktop\MyDestination"; ZipFile.ExtractToDirectory(zipPath, destinationPath); Once you run it, you will see the content of the Zip copied and extracted to the folder. MyDestination Note that if it does not exist. this method creates the destination folder This method accepts two more parameters: , by which you can specify the encoding. . entryNameEncoding The default value is UTF-8 allows you to specify whether it must overwrite existing files. The default value is . If set to false and the destination files already exist, this method throws a saying that the file already exists. overwriteFiles false System.IO.IOException Using C# to create a Zip from a folder The key method here is , which allows you to create Zip files in a flexible way. ZipFile.CreateFromDirectory The first mandatory value is, of course, the . source directory path The second mandatory parameter is the destination of the resulting Zip file. It can be the to the file: local path string sourceFolderPath = @"\Desktop\myFolder"; string destinationZipPath = @"\Desktop\destinationFile.zip"; ZipFile.CreateFromDirectory(sourceFolderPath, destinationZipPath); Or it can be a that you can use later for other operations: Stream using (MemoryStream memStream = new MemoryStream()) { string sourceFolderPath = @"\Desktop\myFolder"; ZipFile.CreateFromDirectory(sourceFolderPath, memStream); var lenght = memStream.Length;// here the Stream is populated } You can finally add some optional parameters: , whose values are , , , . compressionLevel Optimal Fastest NoCompression SmallestSize : a flag that defines if you have to copy only the first-level files or also the root folder. includeBaseDirectory A quick comparison of the four Compression Levels As we just saw, we have four compression levels: , , , and . Optimal Fastest NoCompression SmallestSize What happens if I use the different values to zip all the photos and videos of my latest trip? The source folder’s size is 16.2 GB. Let me zip it with the four compression levels: private long CreateAndTrack(string sourcePath, string destinationPath, CompressionLevel compression) { Stopwatch stopwatch = Stopwatch.StartNew(); ZipFile.CreateFromDirectory( sourceDirectoryName: sourcePath, destinationArchiveFileName: destinationPath, compressionLevel: compression, includeBaseDirectory: true ); stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; } // in Main... var smallestTime = CreateAndTrack(sourceFolderPath, Path.Combine(rootFolder, "Smallest.zip"), CompressionLevel.SmallestSize); var noCompressionTime = CreateAndTrack(sourceFolderPath, Path.Combine(rootFolder, "NoCompression.zip"), CompressionLevel.NoCompression); var fastestTime = CreateAndTrack(sourceFolderPath, Path.Combine(rootFolder, "Fastest.zip"), CompressionLevel.Fastest); var optimalTime = CreateAndTrack(sourceFolderPath, Path.Combine(rootFolder, "Optimal.zip"), CompressionLevel.Optimal); By executing this operation, we have this table: We can see a bunch of weird things: Fastest compression generates a smaller file than Smallest compression. Fastest compression is way slower than Smallest compression. Optimal lies in the middle. This is to say: don’t trust the names; , even with a test as simple as this. remember to benchmark the parts where you need performance Wrapping up This was a quick article about one specific class in the .NET ecosystem. As we saw, even though the class is simple and it’s all about three methods, there are some things you should keep in mind before using this class in your code. I hope you enjoyed this article! Let’s keep in touch on or ! 🤜🤛 Twitter LinkedIn Happy coding! 🐧 Also published . here