ZipFileExtensions Class
![]() |
---|
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience. |
Provides extension methods for the ZipArchive and ZipArchiveEntry classes.
Assembly: Â System.IO.Compression.FileSystem (in System.IO.Compression.FileSystem.dll)
Name | Description | |
---|---|---|
![]() ![]() | CreateEntryFromFile(ZipArchive, String, String) | Archives a file by compressing it and adding it to the zip archive. |
![]() ![]() | CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) | Archives a file by compressing it using the specified compression level and adding it to the zip archive. |
![]() ![]() | ExtractToDirectory(ZipArchive, String) | Extracts all the files in the zip archive to a directory on the file system. |
![]() ![]() | ExtractToFile(ZipArchiveEntry, String) | Extracts an entry in the zip archive to a file. |
![]() ![]() | ExtractToFile(ZipArchiveEntry, String, Boolean) | Extracts an entry in the zip archive to a file, and optionally overwrites an existing file that has the same name. |
The ZipFileExtensions class contains only static methods that extend the ZipArchive and ZipArchiveEntry classes. You do not create an instance of the ZipFileExtensions class; instead, you use these methods from instances of ZipArchive or ZipArchiveEntry.
To use the extension methods, you must reference the System.IO.Compression.FileSystem assembly in your project. The System.IO.Compression.FileSystem assembly is not available in Windows 8.x Store apps. Therefore, the ZipFileExtensions and ZipFile classes (both of which are in the System.IO.Compression.FileSystem assembly) are not available in Windows 8.x Store apps. In Windows 8.x Store apps, you work with compressed files by using the methods in ZipArchive, ZipArchiveEntry, DeflateStream, and GZipStream.
The ZipFileExtensions class contains three methods that extend ZipArchive:
The ZipFileExtensions class contains two methods that extend ZipArchiveEntry:
The following example shows how to create a new entry in a zip archive from an existing file, and extract the contents of the archive to a directory.
using System; using System.IO; using System.IO.Compression; namespace ConsoleApplication { class Program { static void Main(string[] args) { string zipPath = @"c:\users\exampleuser\start.zip"; string extractPath = @"c:\users\exampleuser\extract"; string newFile = @"c:\users\exampleuser\NewFile.txt"; using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update)) { archive.CreateEntryFromFile(newFile, "NewEntry.txt"); archive.ExtractToDirectory(extractPath); } } } }
The following example shows how to iterate through the contents of a zip archive and extract files that have a .txt extension.
using System; using System.IO; using System.IO.Compression; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string zipPath = @"c:\example\start.zip"; string extractPath = @"c:\example\extract"; using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)) { entry.ExtractToFile(Path.Combine(extractPath, entry.FullName)); } } } } } }
Available since 10
.NET Framework
Available since 4.5
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.