ZipFile Example
This page was last reviewed on Apr 27, 2023.
Dot Net Perls
ZipFile. The C# ZipFile class compresses directories. We call CreateFromDirectory and ExtractToDirectory. All files in the directory are compressed to, or expanded to, the directory.
Add reference. In Visual Studio, you may need to add a reference to your project to use ZipFile. Go to the Project menu, Add Reference, and then select System.IO.Compression.FileSystem.
This C# example uses the CreateFromDirectory and ExtractToDirectory methods. It uses hard-coded relative path names—you can change these to make the program work if needed.
Info The directory we want to compress is "source" and the output file is "destination.zip."
And After execution, the folder the program resides in will have a file "destination.zip" and a folder "destination."
Note This program will cause an error if it is run twice. You must delete the previous output files for correct operation.
using System.IO.Compression; class Program { static void Main() { // Create a ZIP file from the directory "source". // ... The "source" folder is in the same directory as this program. // ... Use optimal compression. ZipFile.CreateFromDirectory("source", "destination.zip", CompressionLevel.Optimal, false); // Extract the directory we just created. // ... Store the results in a new folder called "destination". // ... The new folder must not exist. ZipFile.ExtractToDirectory("destination.zip", "destination"); } }
Next, we use the ZipArchiveEntry class. After calling ZipFile.Open, we can loop over the entries within the ZIP file by using the Entries property on the ZipArchive class.
Start Please create a "file.zip" file. In Windows, create a new compressed folder. Drag a file "test.txt" to the new ZIP file to add it.
Tip Make sure you include the System.IO.Compression assembly using Add References in Visual Studio.
Warning The path manipulation code is not ideal. Using Path.Combine, in place of string concatenation, would be superior.
Result The program expands all files from the original ZIP archive. It places them in the same folder where the archive itself is stored.
using System; using System.IO.Compression; class Program { static void Main() { // ... Get the ZipArchive from ZipFile.Open. using (ZipArchive archive = ZipFile.Open(@"C:\folder\file.zip", ZipArchiveMode.Read)) { // ... Loop over entries. foreach (ZipArchiveEntry entry in archive.Entries) { // ... Display properties. // Extract to directory. Console.WriteLine("Size: " + entry.CompressedLength); Console.WriteLine("Name: " + entry.Name); entry.ExtractToFile(@"C:\folder\" + entry.Name); } } } }
Size: 50 Name: test.txt
CompressionLevel. ZipFile can be used with the CompressionLevel enum. In my tests, passing CompressionLevel.Optimal usually resulted in better compression ratios.
A summary. In .NET we can use Deflate, GZIP, different compression levels, and handle entire directories of folders and files. Custom compression libraries and code become less helpful.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 27, 2023 (edit).
© 2007-2024 Sam Allen.