ZipArchiveEntry Sınıf

Tanım

Zip arşivi içindeki sıkıştırılmış bir dosyayı temsil eder.

public ref class ZipArchiveEntry
public class ZipArchiveEntry
type ZipArchiveEntry = class
Public Class ZipArchiveEntry
Devralma
ZipArchiveEntry

Açıklamalar

Zip arşivi, sıkıştırılmış her dosya için bir giriş içerir. ZipArchiveEntry sınıfı, bir girdinin özelliklerini incelemenize ve girişi açmanıza veya silmenize olanak tanır. Bir girdiyi açtığınızda, sıkıştırılmış dosyanın akışına yazarak sıkıştırılmış dosyayı değiştirebilirsiniz.

Zip arşivlerini ve bunların dosya girdilerini düzenleme yöntemleri üç sınıfa yayılır: ZipFile, ZipArchive ve ZipArchiveEntry.

Kime... Kullan...
Dizinden zip arşivi oluşturma ZipFile.CreateFromDirectory
Zip arşivinin içeriğini dizine ayıklama ZipFile.ExtractToDirectory
Mevcut zip arşivine yeni dosyalar ekleme ZipArchive.CreateEntry
Zip arşivinde dosya alma ZipArchive.GetEntry
Zip arşivindeki tüm dosyaları alma ZipArchive.Entries
Zip arşivinde bulunan tek bir dosyaya akış açmak için ZipArchiveEntry.Open
Zip arşivinden dosya silme ZipArchiveEntry.Delete

Projenizdeki derlemeye System.IO.Compression.FileSystem başvurursanız, sınıfı için iki uzantı yöntemine ZipArchiveEntry erişebilirsiniz. Bu yöntemler ve ExtractToFile(ZipArchiveEntry, String, Boolean)şeklindedir ExtractToFile(ZipArchiveEntry, String) ve bir dosyaya girişin içeriğini açmanızı sağlar. Derleme System.IO.Compression.FileSystem Windows 8'de kullanılamaz. Windows 8.x Store uygulamalarında, veya GZipStreamkullanarak DeflateStream bir arşivin içeriğinin sıkıştırmasını açabilir veya Windows Çalışma Zamanı türlerini Compressor kullanarak dosyaları Decompressor sıkıştırıp kaldırabilirsiniz.

Örnekler

İlk örnekte zip arşivinde yeni bir girişin nasıl oluşturulacağı ve bu girdiye nasıl yazacağı gösterilmektedir.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine("Information about this package.");
                            writer.WriteLine("========================");
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Using zipToOpen As FileStream = New FileStream("c:\users\exampleuser\release.zip", FileMode.Open)
            Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
                Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("Readme.txt")
                Using writer As StreamWriter = New StreamWriter(readmeEntry.Open())
                    writer.WriteLine("Information about this package.")
                    writer.WriteLine("========================")
                End Using
            End Using
        End Using
    End Sub

End Module

İkinci örnekte uzantı yönteminin nasıl kullanılacağı gösterilmektedir ExtractToFile(ZipArchiveEntry, String) . Kodun System.IO.Compression.FileSystem yürütülmesi için projenizdeki derlemeye başvurmanız gerekir.

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

Özellikler

Archive

Girdinin ait olduğu zip arşivini alır.

Comment

İsteğe bağlı giriş açıklamasını alır veya ayarlar.

CompressedLength

Zip arşivindeki girdinin sıkıştırılmış boyutunu alır.

Crc32

32 bit Döngüsel Yedekli Denetim.

ExternalAttributes

İşletim sistemi ve uygulamaya özgü dosya öznitelikleri.

FullName

Zip arşivindeki girdinin göreli yolunu alır.

IsEncrypted

Girdinin şifrelenip şifrelenmediğini belirten bir değer alır.

LastWriteTime

Zip arşivindeki girdinin en son ne zaman değiştirildiğini alır veya ayarlar.

Length

Zip arşivindeki girdinin sıkıştırılmamış boyutunu alır.

Name

Zip arşivindeki girdinin dosya adını alır.

Yöntemler

Delete()

Girdiyi zip arşivinden siler.

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
Open()

Zip arşivinden girdiyi açar.

ToString()

Zip arşivindeki girdinin göreli yolunu alır.

Uzantı Metotları

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.

Şunlara uygulanır