ZipArchiveEntry Klasa

Definicja

Reprezentuje skompresowany plik w archiwum zip.

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

Uwagi

Archiwum zip zawiera wpis dla każdego skompresowanego pliku. Klasa ZipArchiveEntry umożliwia sprawdzenie właściwości wpisu i otwarcie lub usunięcie wpisu. Po otwarciu wpisu możesz zmodyfikować skompresowany plik, zapisując go w strumieniu dla tego skompresowanego pliku.

Metody manipulowania archiwami zip i ich wpisy plików są rozłożone na trzy klasy: ZipFile, ZipArchive i ZipArchiveEntry.

Do... Użyj...
Twórca archiwum zip z katalogu ZipFile.CreateFromDirectory
Wyodrębnianie zawartości archiwum zip do katalogu ZipFile.ExtractToDirectory
Dodawanie nowych plików do istniejącego archiwum zip ZipArchive.CreateEntry
Pobieranie pliku w archiwum zip ZipArchive.GetEntry
Pobieranie wszystkich plików w archiwum zip ZipArchive.Entries
Aby otworzyć strumień do pojedynczego pliku zawartego w archiwum zip ZipArchiveEntry.Open
Usuwanie pliku z archiwum zip ZipArchiveEntry.Delete

Jeśli odwołujesz się do System.IO.Compression.FileSystem zestawu w projekcie, możesz uzyskać dostęp do dwóch metod rozszerzenia dla ZipArchiveEntry klasy . Te metody to ExtractToFile(ZipArchiveEntry, String) i ExtractToFile(ZipArchiveEntry, String, Boolean)umożliwiają dekompresję zawartości wpisu do pliku. Zestaw System.IO.Compression.FileSystem nie jest dostępny w Windows 8. W aplikacjach ze sklepu Windows 8.x można dekompresować zawartość archiwum za pomocą narzędzia DeflateStream lub GZipStream, lub użyć typów Compressor środowisko wykonawcze systemu Windows oraz Decompressor do kompresowania i dekompresowania plików.

Przykłady

W pierwszym przykładzie pokazano, jak utworzyć nowy wpis w archiwum zip i zapisać go w nim.

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

W drugim przykładzie pokazano, jak używać ExtractToFile(ZipArchiveEntry, String) metody rozszerzenia. Do wykonania kodu należy odwołać się System.IO.Compression.FileSystem do zestawu w projekcie.

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

Właściwości

Archive

Pobiera archiwum zip, do którego należy wpis.

Comment

Pobiera lub ustawia opcjonalny komentarz wpisu.

CompressedLength

Pobiera skompresowany rozmiar wpisu w archiwum zip.

Crc32

32-bitowe cykliczne sprawdzanie nadmiarowe.

ExternalAttributes

System operacyjny i atrybuty plików specyficzne dla aplikacji.

FullName

Pobiera ścieżkę względną wpisu w archiwum zip.

IsEncrypted

Pobiera wartość wskazującą, czy wpis jest zaszyfrowany.

LastWriteTime

Pobiera lub ustawia ostatni raz wpis w archiwum zip został zmieniony.

Length

Pobiera nieskompresowany rozmiar wpisu w archiwum zip.

Name

Pobiera nazwę pliku wpisu w archiwum zip.

Metody

Delete()

Usuwa wpis z archiwum zip.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
Open()

Otwiera wpis z archiwum zip.

ToString()

Pobiera ścieżkę względną wpisu w archiwum zip.

Metody rozszerzania

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.

Dotyczy