Op Englesch liesen Editéieren

Deelen iwwer


ZipFileExtensions Class

Definition

Provides extension methods for the ZipArchive and ZipArchiveEntry classes.

C#
public static class ZipFileExtensions
Inheritance
ZipFileExtensions

Remarks

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 8.x Store apps. Therefore, the ZipFileExtensions and ZipFile classes (both of which are in the System.IO.Compression.FileSystem assembly) are not available in Windows 8.x Store apps. In Windows 8.x Store apps, you work with compressed files by using the methods in ZipArchive, ZipArchiveEntry, DeflateStream, and GZipStream.

The ZipFileExtensions class contains four methods that extend ZipArchive:

The ZipFileExtensions class contains two methods that extend ZipArchiveEntry:

Examples

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.

C#
using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\users\exampleuser\start.zip";
            string extractPath = @"c:\users\exampleuser\extract";
            string newFile = @"c:\users\exampleuser\NewFile.txt";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(newFile, "NewEntry.txt");
                archive.ExtractToDirectory(extractPath);
            }
        }
    }
}

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

C#
using System;
using System.IO;
using System.IO.Compression;

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

        Console.WriteLine("Provide path where to extract the zip file:");
        string extractPath = Console.ReadLine();

        // Normalizes the path.
        extractPath = Path.GetFullPath(extractPath);

        // Ensures that the last character on the extraction path
        // is the directory separator char.
        // Without this, a malicious zip file could try to traverse outside of the expected
        // extraction path.
        if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
            extractPath += Path.DirectorySeparatorChar;

        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // Gets the full path to ensure that relative segments are removed.
                    string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName));

                    // Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                    // are case-insensitive.
                    if (destinationPath.StartsWith(extractPath, StringComparison.Ordinal))
                        entry.ExtractToFile(destinationPath);
                }
            }
        }
    }
}

Methods

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

Archives a file by compressing it using the specified compression level and adding it to the zip archive.

CreateEntryFromFile(ZipArchive, String, String)

Archives a file by compressing it and adding it to the zip archive.

ExtractToDirectory(ZipArchive, String, Boolean)

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

ExtractToDirectory(ZipArchive, String)

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

ExtractToFile(ZipArchiveEntry, String, Boolean)

Extracts an entry in the zip archive to a file, and optionally overwrites an existing file that has the same name.

ExtractToFile(ZipArchiveEntry, String)

Extracts an entry in the zip archive to a file.

Applies to

Produkt Versiounen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0