ZipFileExtensions.ExtractToFile Method (ZipArchiveEntry, String, Boolean)
Extracts an entry in the zip archive to a file, and optionally overwrites an existing file that has the same name.
Assembly: System.IO.Compression.FileSystem (in System.IO.Compression.FileSystem.dll)
'Declaration <ExtensionAttribute> _ Public Shared Sub ExtractToFile ( _ source As ZipArchiveEntry, _ destinationFileName As String, _ overwrite As Boolean _ )
- 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.
- Type: System.Boolean
true to overwrite an existing file that has the same name as the destination file; otherwise, false.
Usage NoteIn Visual Basic and C#, you can call this method as an instance method on any object of type ZipArchiveEntry. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
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 Nothing.
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 and overwrite is false.
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.
The following example shows how to iterate through the contents of a zip archive file, and extract files that have a .txt extension. It overwrites an existing file that has the same name in the destination folder.
Imports System.IO Imports System.IO.Compression Module Module1 Sub Main() Dim zipPath As String = "c:\example\start.zip" Dim extractPath As String = "c:\example\extract" Using archive As ZipArchive = ZipFile.OpenRead(zipPath) For Each entry As ZipArchiveEntry In archive.Entries If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then entry.ExtractToFile(Path.Combine(extractPath, entry.FullName), True) End If Next End Using End Sub End Module