Share via


ZipFileExtensions.ExtractToFile Yöntem

Tanım

Zip arşivindeki bir girdiyi bir dosyaya ayıklar.

Aşırı Yüklemeler

ExtractToFile(ZipArchiveEntry, String)

Zip arşivindeki bir girdiyi bir dosyaya ayıklar.

ExtractToFile(ZipArchiveEntry, String, Boolean)

Zip arşivindeki bir girdiyi bir dosyaya ayıklar ve isteğe bağlı olarak aynı ada sahip mevcut bir dosyanın üzerine yazar.

ExtractToFile(ZipArchiveEntry, String)

Kaynak:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Kaynak:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Kaynak:
ZipFileExtensions.ZipArchiveEntry.Extract.cs

Zip arşivindeki bir girdiyi bir dosyaya ayıklar.

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)

Parametreler

source
ZipArchiveEntry

Dosyasının ayıklanması için zip arşiv girdisi.

destinationFileName
String

Girdinin içeriğinden oluşturulacak dosyanın yolu. Göreli veya mutlak bir yol belirtebilirsiniz. Göreli yol, geçerli çalışma dizinine göre yorumlanır.

Özel durumlar

destinationFileName sıfır uzunluklu bir dizedir, yalnızca boşluk içerir veya tarafından InvalidPathCharstanımlanan bir veya daha fazla geçersiz karakter içerir.

-veya-

destinationFileName bir dizin belirtir.

destinationFileName, null değeridir.

Belirtilen yol, dosya adı veya her ikisi birden sistem tarafından tanımlanan en fazla uzunluğu aşıyor.

Belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).

destinationFileName zaten var.

-veya-

G/ç hatası oluştu.

-veya-

Girdi şu anda yazmak için açık.

-veya-

Girdi arşivden silindi.

Çağıranın yeni dosyayı oluşturmak için gerekli izni yok.

Girdi arşivde eksik veya bozuk ve okunamıyor.

-veya-

Giriş, desteklenmeyen bir sıkıştırma yöntemi kullanılarak sıkıştırıldı.

Bu girdinin ait olduğu zip arşivi atılmış.

destinationFileName geçersiz biçimde.

-veya-

Bu girdinin zip arşivi, girdilerin alınmasına izin vermeyen modda Create açıldı.

Örnekler

Aşağıdaki örnekte, zip arşiv dosyasının içeriğinde yineleme ve .txt uzantısına sahip dosyaları ayıklama gösterilmektedir.

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

Açıklamalar

Hedef dosya zaten varsa, bu yöntem dosyanın üzerine yazmaz; bir IOException özel durum oluşturur. Varolan bir dosyanın üzerine yazmak için bunun yerine yöntem aşırı yüklemesini ExtractToFile(ZipArchiveEntry, String, Boolean) kullanın.

Dosyanın son yazma zamanı, zip arşivindeki girdinin son değiştirildiği zamana ayarlanır; bu değer özelliğinde LastWriteTime depolanır.

Dizin ayıklamak için bu yöntemi kullanamazsınız; ExtractToDirectory bunun yerine yöntemini kullanın.

Şunlara uygulanır

ExtractToFile(ZipArchiveEntry, String, Boolean)

Kaynak:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Kaynak:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Kaynak:
ZipFileExtensions.ZipArchiveEntry.Extract.cs

Zip arşivindeki bir girdiyi bir dosyaya ayıklar ve isteğe bağlı olarak aynı ada sahip mevcut bir dosyanın üzerine yazar.

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)

Parametreler

source
ZipArchiveEntry

Dosyasının ayıklanması için zip arşiv girdisi.

destinationFileName
String

Girdinin içeriğinden oluşturulacak dosyanın yolu. Göreli veya mutlak bir yol belirtebilirsiniz. Göreli yol, geçerli çalışma dizinine göre yorumlanır.

overwrite
Boolean

true hedef dosyayla aynı ada sahip mevcut bir dosyanın üzerine yazmak için; aksi takdirde , false.

Özel durumlar

destinationFileName sıfır uzunluklu bir dizedir, yalnızca boşluk içerir veya tarafından InvalidPathCharstanımlanan bir veya daha fazla geçersiz karakter içerir.

-veya-

destinationFileName bir dizin belirtir.

destinationFileName, null değeridir.

Belirtilen yol, dosya adı veya her ikisi birden sistem tarafından tanımlanan en fazla uzunluğu aşıyor.

Belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).

destinationFileName zaten var ve overwrite şeklindedir false.

-veya-

G/ç hatası oluştu.

-veya-

Girdi şu anda yazmak için açık.

-veya-

Girdi arşivden silindi.

Çağıranın yeni dosyayı oluşturmak için gerekli izni yok.

Girdi arşivde eksik veya bozuk ve okunamıyor.

-veya-

Giriş, desteklenmeyen bir sıkıştırma yöntemi kullanılarak sıkıştırıldı.

Bu girdinin ait olduğu zip arşivi atılmış.

destinationFileName geçersiz biçimde.

-veya-

Bu girdinin zip arşivi, girdilerin alınmasına izin vermeyen modda Create açıldı.

Örnekler

Aşağıdaki örnekte zip arşiv dosyasının içeriğinde yineleme ve .txt uzantısına sahip dosyaları ayıklama gösterilmektedir. Hedef klasörde aynı ada sahip mevcut bir dosyanın üzerine yazar. Bu kod örneğini derleyicik için projenizdeki ve System.IO.Compression.FileSystem derlemelerine başvurmanız System.IO.Compression gerekir.

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

Açıklamalar

Dosyanın son yazma zamanı, zip arşivindeki girdinin son değiştirildiği zamana ayarlanır; bu değer özelliğinde LastWriteTime depolanır.

Dizin ayıklamak için bu yöntemi kullanamazsınız; ExtractToDirectory bunun yerine yöntemini kullanın.

Şunlara uygulanır