Edit

Share via


ZipFile Class

Definition

Provides static methods for creating, extracting, and opening zip archives.

public ref class ZipFile abstract sealed
public static class ZipFile
type ZipFile = class
Public Class ZipFile
Inheritance
ZipFile

Remarks

Note

To use the ZipFile class in a .NET Framework app, you must add a reference to the System.IO.Compression.FileSystem assembly in your project. For information on how to add a reference to your project in Visual Studio, see How to: Add or Remove References.

The methods for manipulating zip archives and their files are spread across three classes: ZipFile, ZipArchive, and ZipArchiveEntry.

To... Use...
Create a zip archive from a directory ZipFile.CreateFromDirectory
Extract the contents of a zip archive to a directory ZipFile.ExtractToDirectory
Add new files to an existing zip archive ZipArchive.CreateEntry
Retrieve a file in a zip archive ZipArchive.GetEntry
Retrieve all of the files in a zip archive ZipArchive.Entries
Open a stream to an individual file contained in a zip archive ZipArchiveEntry.Open
Delete a file from a zip archive ZipArchiveEntry.Delete

Examples

This example shows how to create and extract a zip archive by using the ZipFile class. It compresses the contents of a folder into a zip archive, and then extracts that content to a new folder.

using System;
using System.IO.Compression;

class Program
{
    static void Main(string[] args)
    {
        string startPath = @".\start";
        string zipPath = @".\result.zip";
        string extractPath = @".\extract";

        ZipFile.CreateFromDirectory(startPath, zipPath);

        ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
}
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim startPath As String = ".\start"
        Dim zipPath As String = ".\result.zip"
        Dim extractPath As String = ".\extract"

        ZipFile.CreateFromDirectory(startPath, zipPath)

        ZipFile.ExtractToDirectory(zipPath, extractPath)
    End Sub

End Module

Methods

CreateFromDirectory(String, Stream, CompressionLevel, Boolean, Encoding)

Creates a zip archive in the specified stream that contains the files and directories from the specified directory, uses the specified compression level and character encoding for entry names and comments, and optionally includes the base directory.

CreateFromDirectory(String, Stream, CompressionLevel, Boolean)

Creates a zip archive in the specified stream that contains the files and directories from the specified directory, uses the specified compression level, and optionally includes the base directory.

CreateFromDirectory(String, Stream)

Creates a zip archive in the specified stream that contains the files and directories from the specified directory.

CreateFromDirectory(String, String, CompressionLevel, Boolean, Encoding)

Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level and character encoding for entry names and comments, and optionally includes the base directory.

CreateFromDirectory(String, String, CompressionLevel, Boolean)

Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level, and optionally includes the base directory.

CreateFromDirectory(String, String)

Creates a zip archive that contains the files and directories from the specified directory.

CreateFromDirectoryAsync(String, Stream, CancellationToken)

Asynchronously creates a zip archive in the specified stream that contains the files and directories from the specified directory.

CreateFromDirectoryAsync(String, Stream, CompressionLevel, Boolean, CancellationToken)

Asynchronously creates a zip archive in the specified stream that contains the files and directories from the specified directory, using the specified compression level, and optionally including the base directory.

CreateFromDirectoryAsync(String, Stream, CompressionLevel, Boolean, Encoding, CancellationToken)

Asynchronously creates a zip archive in the specified stream that contains the files and directories from the specified directory, using the specified compression level and character encoding for entry names, and optionally including the base directory.

CreateFromDirectoryAsync(String, String, CancellationToken)

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories from the directory specified by sourceDirectoryName.

CreateFromDirectoryAsync(String, String, CompressionLevel, Boolean, CancellationToken)

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName.

CreateFromDirectoryAsync(String, String, CompressionLevel, Boolean, Encoding, CancellationToken)

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName.

ExtractToDirectory(Stream, String, Boolean)

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system, and optionally allows choosing if the files in the destination directory should be overwritten.

ExtractToDirectory(Stream, String, Encoding, Boolean)

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system, uses the specified character encoding for entry names and comments, and optionally allows choosing if the files in the destination directory should be overwritten.

ExtractToDirectory(Stream, String, Encoding)

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system and uses the specified character encoding for entry names and comments.

ExtractToDirectory(Stream, String)

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system.

ExtractToDirectory(String, String, Boolean)

Extracts all of the files in the specified archive to a directory on the file system.

ExtractToDirectory(String, String, Encoding, Boolean)

Extracts all of the files in the specified archive to a directory on the file system.

ExtractToDirectory(String, String, Encoding)

Extracts all the files in the specified zip archive to a directory on the file system and uses the specified character encoding for entry names and comments.

ExtractToDirectory(String, String)

Extracts all the files in the specified zip archive to a directory on the file system.

ExtractToDirectoryAsync(Stream, String, Boolean, CancellationToken)

Asynchronously extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system, and optionally allows choosing if the files in the destination directory should be overwritten.

ExtractToDirectoryAsync(Stream, String, CancellationToken)

Asynchronously extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system.

ExtractToDirectoryAsync(Stream, String, Encoding, Boolean, CancellationToken)

Asynchronously extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system, uses the specified character encoding for entry names, and optionally allows choosing if the files in the destination directory should be overwritten.

ExtractToDirectoryAsync(Stream, String, Encoding, CancellationToken)

Asynchronously extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system and uses the specified character encoding for entry names.

ExtractToDirectoryAsync(String, String, Boolean, CancellationToken)

Asynchronously extracts all of the files in the specified archive to a directory on the file system.

ExtractToDirectoryAsync(String, String, CancellationToken)

Asynchronously extracts all of the files in the specified archive to a directory on the file system.

ExtractToDirectoryAsync(String, String, Encoding, Boolean, CancellationToken)

Asynchronously extracts all of the files in the specified archive to a directory on the file system.

The specified directory must not exist. This method will create all subdirectories and the specified directory.

If there is an error while extracting the archive, the archive will remain partially extracted. Each entry will be extracted such that the extracted file has the same relative path to the destinationDirectoryName as the entry has to the archive. The path is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file to be archived has an invalid last modified time, the first datetime representable in the zip timestamp format (midnight on January 1, 1980) will be used.

ExtractToDirectoryAsync(String, String, Encoding, CancellationToken)

Asynchronously extracts all of the files in the specified archive to a directory on the file system.

Open(String, ZipArchiveMode, Encoding)

Opens a zip archive at the specified path, in the specified mode, and by using the specified character encoding for entry names and comments.

Open(String, ZipArchiveMode)

Opens a zip archive at the specified path and in the specified mode.

OpenAsync(String, ZipArchiveMode, CancellationToken)

Asynchronously opens a ZipArchive on the specified archiveFileName in the specified ZipArchiveMode mode.

OpenAsync(String, ZipArchiveMode, Encoding, CancellationToken)

Asynchronously opens a ZipArchive on the specified archiveFileName in the specified ZipArchiveMode mode.

OpenRead(String)

Opens a zip archive for reading at the specified path.

OpenReadAsync(String, CancellationToken)

Asynchronously opens a ZipArchive on the specified path for reading. The specified file is opened with FileMode.Open.

Applies to