Back to Devexpress

How to: Unzip an Archive

officefileapi-15247-zip-compression-and-archive-api-examples-how-to-unzip-an-archive.md

latest2.0 KB
Original Source

How to: Unzip an Archive

  • Sep 19, 2023
  1. Call the static ZipArchive.Read method of the DevExpress.Compression.ZipArchive class. It has two overloads, so you can specify a compressed stream or a path to archive a file as the method’s argument. This method returns a ZipArchive instance that provides access to a collection of ZipItem elements using indexed notation.
  2. An archive item can be accessed using its archive path or index.
  3. You can specify the ZipArchive.Password property value, if required.
  4. For each ZipItem call the ZipItem.Extract method. It has two overloads, so you can either specify a destination stream or a destination file path.

This example illustrates how to load the zip file and extract it to the specified directory. If the target directory is not specified, the current directory is the target.

View Example

csharp
using DevExpress.Compression;

public void UnzipArchive() {
    string pathToZipArchive = "Documents\\Example.zip";
    string pathToExtract = "Documents\\!Extracted";
    using (ZipArchive archive = ZipArchive.Read(pathToZipArchive)) {
        foreach (ZipItem item in archive) {
            item.Extract(pathToExtract);
        }
    }
}
vb
Imports DevExpress.Compression

Public Sub UnzipArchive()
    Dim pathToZipArchive As String = "Documents\Example.zip"
    Dim pathToExtract As String = "Documents\!Extracted"
    Using archive As ZipArchive = ZipArchive.Read(pathToZipArchive)
        For Each item As ZipItem In archive
            item.Extract(pathToExtract)
        Next item
    End Using
End Sub