ZipFileExtensions Klass

Definition

Tillhandahåller tilläggsmetoder för klasserna ZipArchive och ZipArchiveEntry .

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
Arv
ZipFileExtensions

Kommentarer

Klassen ZipFileExtensions innehåller endast statiska metoder som utökar klasserna ZipArchive och ZipArchiveEntry . Du skapar inte en instans av ZipFileExtensions klassen. I stället använder du dessa metoder från instanser av ZipArchive eller ZipArchiveEntry.

Om du vill använda tilläggsmetoderna måste du referera till System.IO.Compression.FileSystem sammansättningen i projektet. Sammansättningen System.IO.Compression.FileSystem är inte tillgänglig i Windows 8.x Store-appar. Därför är klasserna ZipFileExtensions och ZipFile (som båda finns i sammansättningen System.IO.Compression.FileSystem) inte tillgängliga i Windows 8.x Store-appar. I Windows 8.x Store-appar arbetar du med komprimerade filer med hjälp av metoderna i ZipArchive, ZipArchiveEntry, DeflateStream och GZipStream.

Klassen ZipFileExtensions innehåller fyra metoder som utökar ZipArchive:

Klassen ZipFileExtensions innehåller två metoder som utökar ZipArchiveEntry:

Exempel

I följande exempel visas hur du skapar en ny post i ett zip-arkiv från en befintlig fil och extraherar innehållet i arkivet till en katalog.

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);
            }
        }
    }
}
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

I följande exempel visas hur du itererar genom innehållet i ett zip-arkiv och extraherar filer som har ett .txt-tillägg.

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);
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = ".\result.zip"

        Console.WriteLine("Provide path where to extract the zip file:")
        Dim extractPath As String = 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 Not extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) Then
            extractPath += Path.DirectorySeparatorChar
        End If

        Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
            For Each entry As ZipArchiveEntry In archive.Entries
                If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then

                    ' Gets the full path to ensure that relative segments are removed.
                    Dim destinationPath As String = 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) Then 
                        entry.ExtractToFile(destinationPath)
                    End If

                End If
            Next
        End Using
    End Sub

End Module

Metoder

Name Description
CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

Arkiverar en fil genom att komprimera den med den angivna komprimeringsnivån och lägga till den i zip-arkivet.

CreateEntryFromFile(ZipArchive, String, String)

Arkiverar en fil genom att komprimera den och lägga till den i zip-arkivet.

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

Lägger asynkront till en fil från filsystemet i arkivet under det angivna postnamnet.

CreateEntryFromFileAsync(ZipArchive, String, String, CompressionLevel, CancellationToken)

Lägger asynkront till en fil från filsystemet i arkivet under det angivna postnamnet.

ExtractToDirectory(ZipArchive, String, Boolean)

Extraherar alla filer i arkivet till en katalog i filsystemet.

ExtractToDirectory(ZipArchive, String)

Extraherar alla filer i zip-arkivet till en katalog i filsystemet.

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

Extraherar alla filer i arkivet till en katalog i filsystemet. Den angivna katalogen kanske redan finns.

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

Extraherar asynkront alla filer i arkivet till en katalog i filsystemet. Den angivna katalogen kan redan finnas.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Extraherar en post i zip-arkivet till en fil och skriver eventuellt över en befintlig fil som har samma namn.

ExtractToFile(ZipArchiveEntry, String)

Extraherar en post i zip-arkivet till en fil.

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

Asynkront skapar en fil i filsystemet med postens innehåll och det angivna namnet.

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

Asynkront skapar en fil i filsystemet med postens innehåll och det angivna namnet.

Gäller för