ZipFileExtensions.ExtractToFile Method (ZipArchiveEntry, String)

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Extracts an entry in the zip archive to a file.

Namespace:   System.IO.Compression
Assembly:  System.IO.Compression.FileSystem (in System.IO.Compression.FileSystem.dll)

public static void ExtractToFile(
	this ZipArchiveEntry source,
	string destinationFileName


Type: System.IO.Compression.ZipArchiveEntry

The zip archive entry to extract a file from.

Type: System.String

The path of the file to create from the contents of the entry. You can specify either a relative or an absolute path. A relative path is interpreted as relative to the current working directory.

Exception Condition

destinationFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.


destinationFileName specifies a directory.


destinationFileName is null.


The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must not exceed 248 characters, and file names must not exceed 260 characters.


The specified path is invalid (for example, it is on an unmapped drive).


destinationFileName already exists.


An I/O error occurred.


The entry is currently open for writing.


The entry has been deleted from the archive.


The caller does not have the required permission to create the new file.


The entry is missing from the archive, or is corrupt and cannot be read.


The entry has been compressed by using a compression method that is not supported.


The zip archive that this entry belongs to has been disposed.


destinationFileName is in an invalid format.


The zip archive for this entry was opened in Create mode, which does not permit the retrieval of entries.

If the destination file already exists, this method does not overwrite it; it throws an IOException exception. To overwrite an existing file, use the ExtractToFile(ZipArchiveEntry, String, Boolean) method overload instead.

The last write time of the file is set to the last time the entry in the zip archive was changed; this value is stored in the LastWriteTime property.

You cannot use this method to extract a directory; use the ExtractToDirectory method instead.

The following example shows how to iterate through the contents of a zip archive file 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\";
            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));

Universal Windows Platform
Available since 10
.NET Framework
Available since 4.5
Return to top