ZipArchiveEntry Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Představuje komprimovaný soubor v archivu zip.
public ref class ZipArchiveEntry
public class ZipArchiveEntry
type ZipArchiveEntry = class
Public Class ZipArchiveEntry
- Dědičnost
-
ZipArchiveEntry
Poznámky
Archiv zip obsahuje položku pro každý komprimovaný soubor. Třída ZipArchiveEntry umožňuje prozkoumat vlastnosti položky a otevřít nebo odstranit položku. Když otevřete položku, můžete komprimovaný soubor upravit zápisem do datového proudu pro tento komprimovaný soubor.
Metody pro manipulaci s archivy zip a jejich položkami souboru jsou rozloženy do tří tříd: ZipFile, ZipArchive a ZipArchiveEntry.
Komu... | Můžete... |
---|---|
Create archivu zip z adresáře | ZipFile.CreateFromDirectory |
Extrahování obsahu archivu zip do adresáře | ZipFile.ExtractToDirectory |
Přidání nových souborů do existujícího archivu ZIP | ZipArchive.CreateEntry |
Načtení souboru v archivu ZIP | ZipArchive.GetEntry |
Načtení všech souborů z archivu ZIP | ZipArchive.Entries |
Otevření datového proudu pro jednotlivé soubory obsažené v archivu zip | ZipArchiveEntry.Open |
Odstranění souboru z archivu ZIP | ZipArchiveEntry.Delete |
Pokud odkazujete na System.IO.Compression.FileSystem
sestavení v projektu, můžete pro třídu získat přístup ke dvěma rozšiřujícím ZipArchiveEntry metodám. Tyto metody jsou ExtractToFile(ZipArchiveEntry, String) a ExtractToFile(ZipArchiveEntry, String, Boolean)a umožňují dekomprimovat obsah položky do souboru. Sestavení System.IO.Compression.FileSystem
není k dispozici v Windows 8. V aplikacích Windows 8.x Store můžete obsah archivu dekomprimovat pomocí nebo DeflateStreamGZipStreamnebo můžete použít typy prostředí Windows Runtime Compressor a Decompressor ke komprimaci a dekomprimaci souborů.
Příklady
První příklad ukazuje, jak vytvořit novou položku v archivu ZIP a zapisovat do něj.
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
Druhý příklad ukazuje, jak použít rozšiřující metodu ExtractToFile(ZipArchiveEntry, String) . Aby se kód spustil, musíte odkazovat System.IO.Compression.FileSystem
na sestavení v projektu.
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
Vlastnosti
Archive |
Získá archiv ZIP, do kterého položka patří. |
Comment |
Získá nebo nastaví volitelný komentář položky. |
CompressedLength |
Získá komprimovanou velikost položky v archivu ZIP. |
Crc32 |
32bitová cyklické redundantní kontrola. |
ExternalAttributes |
Atributy souborů specifické pro operační systém a aplikaci. |
FullName |
Získá relativní cestu k položce v archivu ZIP. |
IsEncrypted |
Získá hodnotu, která označuje, zda je položka zašifrována. |
LastWriteTime |
Získá nebo nastaví čas poslední položky v archivu zip byla změněna. |
Length |
Získá nekomprimovanou velikost položky v archivu zip. |
Name |
Získá název souboru položky v zip archivu. |
Metody
Delete() |
Odstraní položku z archivu zip. |
Equals(Object) |
Určí, zda se zadaný objekt rovná aktuálnímu objektu. (Zděděno od Object) |
GetHashCode() |
Slouží jako výchozí hashovací funkce. (Zděděno od Object) |
GetType() |
Type Získá z aktuální instance. (Zděděno od Object) |
MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Objectsouboru . (Zděděno od Object) |
Open() |
Otevře položku z archivu zip. |
ToString() |
Načte relativní cestu k položce v archivu zip. |
Metody rozšíření
ExtractToFile(ZipArchiveEntry, String) |
Extrahuje položku v archivu zip do souboru. |
ExtractToFile(ZipArchiveEntry, String, Boolean) |
Extrahuje položku v archivu zip do souboru a volitelně přepíše existující soubor se stejným názvem. |
Platí pro
Váš názor
https://aka.ms/ContentUserFeedback.
Připravujeme: V průběhu roku 2024 budeme postupně vyřazovat problémy z GitHub coby mechanismus zpětné vazby pro obsah a nahrazovat ho novým systémem zpětné vazby. Další informace naleznete v tématu:Odeslat a zobrazit názory pro