ZipFileExtensions.ExtractToFile Metoda

Definicja

Wyodrębnia wpis w archiwum zip do pliku.

Przeciążenia

ExtractToFile(ZipArchiveEntry, String)

Wyodrębnia wpis w archiwum zip do pliku.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Wyodrębnia wpis w archiwum zip do pliku i opcjonalnie zastępuje istniejący plik o tej samej nazwie.

ExtractToFile(ZipArchiveEntry, String)

Źródło:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Źródło:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Źródło:
ZipFileExtensions.ZipArchiveEntry.Extract.cs

Wyodrębnia wpis w archiwum zip do pliku.

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)

Parametry

source
ZipArchiveEntry

Wpis archiwum zip w celu wyodrębnienia pliku.

destinationFileName
String

Ścieżka pliku do utworzenia na podstawie zawartości wpisu. Można określić względną lub bezwzględną ścieżkę. Ścieżka względna jest interpretowana jako względna względem bieżącego katalogu roboczego.

Wyjątki

destinationFileName jest ciągiem o zerowej długości, zawiera tylko białe znaki lub zawiera co najmniej jeden nieprawidłowy znak zdefiniowany przez InvalidPathCharsmetodę .

-lub-

destinationFileName określa katalog.

destinationFileName to null.

Podana ścieżka, nazwa pliku lub obie przekraczają maksymalną długość zdefiniowaną przez system.

Określona ścieżka jest nieprawidłowa (na przykład znajduje się na niezamapowanym dysku).

destinationFileName już istnieje.

-lub-

Wystąpił błąd we/wy.

-lub-

Wpis jest obecnie otwarty do pisania.

-lub-

Wpis został usunięty z archiwum.

Obiekt wywołujący nie ma wymaganych uprawnień do utworzenia nowego pliku.

Brak wpisu z archiwum lub jest uszkodzony i nie można go odczytać.

-lub-

Wpis został skompresowany przy użyciu metody kompresji, która nie jest obsługiwana.

Archiwum zip, do którego należy ten wpis, zostało usunięte.

destinationFileName jest w nieprawidłowym formacie.

-lub-

Archiwum zip dla tego wpisu zostało otwarte w Create trybie, który nie zezwala na pobieranie wpisów.

Przykłady

W poniższym przykładzie pokazano, jak iterować zawartość pliku archiwum zip i wyodrębniać pliki z rozszerzeniem .txt.

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

Uwagi

Jeśli plik docelowy już istnieje, ta metoda nie zastępuje go; zgłasza IOException wyjątek. Aby zastąpić istniejący plik, użyj ExtractToFile(ZipArchiveEntry, String, Boolean) przeciążenia metody .

Czas ostatniego zapisu pliku jest ustawiony na ostatni raz, gdy wpis w archiwum zip został zmieniony; ta wartość jest przechowywana we LastWriteTime właściwości .

Nie można użyć tej metody do wyodrębnienia katalogu; ExtractToDirectory Zamiast tego należy użyć metody .

Dotyczy

ExtractToFile(ZipArchiveEntry, String, Boolean)

Źródło:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Źródło:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Źródło:
ZipFileExtensions.ZipArchiveEntry.Extract.cs

Wyodrębnia wpis w archiwum zip do pliku i opcjonalnie zastępuje istniejący plik o tej samej nazwie.

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)

Parametry

source
ZipArchiveEntry

Wpis archiwum zip w celu wyodrębnienia pliku.

destinationFileName
String

Ścieżka pliku do utworzenia na podstawie zawartości wpisu. Można określić względną lub bezwzględną ścieżkę. Ścieżka względna jest interpretowana jako względna względem bieżącego katalogu roboczego.

overwrite
Boolean

true aby zastąpić istniejący plik o takiej samej nazwie jak plik docelowy; w przeciwnym razie , false.

Wyjątki

destinationFileName jest ciągiem o zerowej długości, zawiera tylko białe znaki lub zawiera co najmniej jeden nieprawidłowy znak zdefiniowany przez InvalidPathCharsmetodę .

-lub-

destinationFileName określa katalog.

destinationFileName to null.

Podana ścieżka, nazwa pliku lub obie przekraczają maksymalną długość zdefiniowaną przez system.

Określona ścieżka jest nieprawidłowa (na przykład znajduje się na niezamapowanym dysku).

destinationFileName już istnieje i overwrite jest false.

-lub-

Wystąpił błąd we/wy.

-lub-

Wpis jest obecnie otwarty do pisania.

-lub-

Wpis został usunięty z archiwum.

Obiekt wywołujący nie ma wymaganych uprawnień do utworzenia nowego pliku.

Brak wpisu z archiwum lub jest uszkodzony i nie można go odczytać.

-lub-

Wpis został skompresowany przy użyciu metody kompresji, która nie jest obsługiwana.

Archiwum zip, do którego należy ten wpis, zostało usunięte.

destinationFileName jest w nieprawidłowym formacie.

-lub-

Archiwum zip dla tego wpisu zostało otwarte w Create trybie, który nie zezwala na pobieranie wpisów.

Przykłady

W poniższym przykładzie pokazano, jak iterować zawartość pliku archiwum zip i wyodrębniać pliki z rozszerzeniem .txt. Zastępuje istniejący plik o tej samej nazwie w folderze docelowym. Aby kompilować ten przykład kodu, należy odwołać się do System.IO.Compression zestawów i System.IO.Compression.FileSystem w projekcie.

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

Uwagi

Czas ostatniego zapisu pliku jest ustawiony na ostatni raz, gdy wpis w archiwum zip został zmieniony; ta wartość jest przechowywana we LastWriteTime właściwości .

Nie można użyć tej metody do wyodrębnienia katalogu; ExtractToDirectory Zamiast tego należy użyć metody .

Dotyczy