ZipFileExtensions.ExtractToFile Methode

Definitie

Extraheert een vermelding in het zip-archief naar een bestand.

Overloads

Name Description
ExtractToFile(ZipArchiveEntry, String)

Extraheert een vermelding in het zip-archief naar een bestand.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Extraheert een vermelding in het zip-archief naar een bestand en overschrijft eventueel een bestaand bestand met dezelfde naam.

ExtractToFile(ZipArchiveEntry, String)

Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs

Extraheert een vermelding in het zip-archief naar een bestand.

public:
[System::Runtime::CompilerServices::Extension]
 static void ExtractToFile(System::IO::Compression::ZipArchiveEntry ^ source, System::String ^ destinationFileName);
public static void ExtractToFile(this System.IO.Compression.ZipArchiveEntry source, string destinationFileName);
static member ExtractToFile : System.IO.Compression.ZipArchiveEntry * string -> unit
<Extension()>
Public Sub ExtractToFile (source As ZipArchiveEntry, destinationFileName As String)

Parameters

source
ZipArchiveEntry

De zip-archiefvermelding waaruit een bestand moet worden geëxtraheerd.

destinationFileName
String

Het pad van het bestand dat moet worden gemaakt op basis van de inhoud van de vermelding. U kunt een relatief of een absoluut pad opgeven. Een relatief pad wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap.

Uitzonderingen

destinationFileName is een tekenreeks met lengte nul, bevat alleen witruimte of bevat een of meer ongeldige tekens zoals gedefinieerd door InvalidPathChars.

– of –

destinationFileName hiermee geeft u een map.

destinationFileName is null.

Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.

Het opgegeven pad is ongeldig (bijvoorbeeld op een niet-toegewezen station).

destinationFileName bestaat al.

– of –

Er is een I/O-fout opgetreden.

– of –

De vermelding is momenteel geopend voor schrijven.

– of –

De vermelding is verwijderd uit het archief.

De aanroeper heeft niet de vereiste machtiging om het nieuwe bestand te maken.

De vermelding ontbreekt in het archief of is beschadigd en kan niet worden gelezen.

– of –

De vermelding is gecomprimeerd met behulp van een compressiemethode die niet wordt ondersteund.

Het zip-archief waartoe deze vermelding behoort, is verwijderd.

destinationFileName heeft een ongeldige indeling.

– of –

Het zip-archief voor deze vermelding is geopend in Create de modus, waardoor het ophalen van items niet mogelijk is.

Voorbeelden

In het volgende voorbeeld ziet u hoe u de inhoud van een zip-archiefbestand kunt herhalen en bestanden kunt extraheren met een .txt extensie.

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

Opmerkingen

Als het doelbestand al bestaat, overschrijft deze methode het niet; het genereert een IOException uitzondering. Als u een bestaand bestand wilt overschrijven, gebruikt u in plaats daarvan de overbelasting van de ExtractToFile(ZipArchiveEntry, String, Boolean) methode.

De laatste schrijftijd van het bestand is ingesteld op de laatste keer dat de vermelding in het zip-archief is gewijzigd; deze waarde wordt opgeslagen in de LastWriteTime eigenschap.

U kunt deze methode niet gebruiken om een map te extraheren; gebruik in plaats daarvan de ExtractToDirectory methode.

Van toepassing op

ExtractToFile(ZipArchiveEntry, String, Boolean)

Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Bron:
ZipFileExtensions.ZipArchiveEntry.Extract.cs

Extraheert een vermelding in het zip-archief naar een bestand en overschrijft eventueel een bestaand bestand met dezelfde naam.

public:
[System::Runtime::CompilerServices::Extension]
 static void ExtractToFile(System::IO::Compression::ZipArchiveEntry ^ source, System::String ^ destinationFileName, bool overwrite);
public static void ExtractToFile(this System.IO.Compression.ZipArchiveEntry source, string destinationFileName, bool overwrite);
static member ExtractToFile : System.IO.Compression.ZipArchiveEntry * string * bool -> unit
<Extension()>
Public Sub ExtractToFile (source As ZipArchiveEntry, destinationFileName As String, overwrite As Boolean)

Parameters

source
ZipArchiveEntry

De zip-archiefvermelding waaruit een bestand moet worden geëxtraheerd.

destinationFileName
String

Het pad van het bestand dat moet worden gemaakt op basis van de inhoud van de vermelding. U kunt een relatief of een absoluut pad opgeven. Een relatief pad wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap.

overwrite
Boolean

true om een bestaand bestand met dezelfde naam als het doelbestand te overschrijven; anders, false.

Uitzonderingen

destinationFileName is een tekenreeks met lengte nul, bevat alleen witruimte of bevat een of meer ongeldige tekens zoals gedefinieerd door InvalidPathChars.

– of –

destinationFileName hiermee geeft u een map.

destinationFileName is null.

Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.

Het opgegeven pad is ongeldig (bijvoorbeeld op een niet-toegewezen station).

destinationFileName bestaat al en overwrite is false.

– of –

Er is een I/O-fout opgetreden.

– of –

De vermelding is momenteel geopend voor schrijven.

– of –

De vermelding is verwijderd uit het archief.

De aanroeper heeft niet de vereiste machtiging om het nieuwe bestand te maken.

De vermelding ontbreekt in het archief of is beschadigd en kan niet worden gelezen.

– of –

De vermelding is gecomprimeerd met behulp van een compressiemethode die niet wordt ondersteund.

Het zip-archief waartoe deze vermelding behoort, is verwijderd.

destinationFileName heeft een ongeldige indeling.

– of –

Het zip-archief voor deze vermelding is geopend in Create de modus, waardoor het ophalen van items niet mogelijk is.

Voorbeelden

In het volgende voorbeeld ziet u hoe u de inhoud van een zip-archiefbestand kunt herhalen en bestanden kunt extraheren die een .txt extensie hebben. Er wordt een bestaand bestand met dezelfde naam in de doelmap overschreven. Als u dit codevoorbeeld wilt compileren, moet u verwijzen naar de System.IO.Compression assembly's in System.IO.Compression.FileSystem uw project.

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\example\start.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))
                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, true);
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\example\start.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) 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, true)
                    End If

                End If
            Next
        End Using
    End Sub

End Module

Opmerkingen

De laatste schrijftijd van het bestand is ingesteld op de laatste keer dat de vermelding in het zip-archief is gewijzigd; deze waarde wordt opgeslagen in de LastWriteTime eigenschap.

U kunt deze methode niet gebruiken om een map te extraheren; gebruik in plaats daarvan de ExtractToDirectory methode.

Van toepassing op