Export (0) Print
Expand All

ZipFileExtensions Class

.NET Framework 4.5

Provides extension methods for the ZipArchive and ZipArchiveEntry classes.

System.Object
  System.IO.Compression.ZipFileExtensions

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

'Declaration
<ExtensionAttribute> _
Public NotInheritable Class ZipFileExtensions

The ZipFileExtensions type exposes the following members.

  NameDescription
Public methodStatic memberCreateEntryFromFile(ZipArchive, String, String)Archives a file by compressing it and adding it to the zip archive.
Public methodStatic memberCreateEntryFromFile(ZipArchive, String, String, CompressionLevel)Archives a file by compressing it using the specified compression level and adding it to the zip archive.
Public methodStatic memberExtractToDirectoryExtracts all the files in the zip archive to a directory on the file system.
Public methodStatic memberExtractToFile(ZipArchiveEntry, String)Extracts an entry in the zip archive to a file.
Public methodStatic memberExtractToFile(ZipArchiveEntry, String, Boolean)Extracts an entry in the zip archive to a file, and optionally overwrites an existing file that has the same name.
Top

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 Store apps. Therefore, the ZipFileExtensions and ZipFile classes (both of which are in the System.IO.Compression.FileSystem assembly) are not available in Windows Store apps. In Windows 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.

Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\users\exampleuser\end.zip" 
        Dim extractPath As String = "c:\users\exampleuser\extract" 
        Dim newFile As String = "c:\users\exampleuser\NewFile.txt" 

        Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
            archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
            archive.ExtractToDirectory(extractPath)
        End Using 
    End Sub 

End Module

The following example shows how to iterate through the contents of a zip archive and extract files that have a .txt extension.

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))
                End If 
            Next 
        End Using 
    End Sub 

End Module

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft