ZipFileExtensions 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供 ZipArchive 和 ZipArchiveEntry 類別的擴充方法。
public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
- 繼承
-
ZipFileExtensions
備註
類別ZipFileExtensions只包含擴充 和 ZipArchiveEntry 類別的ZipArchive靜態方法。 您不會建立 類別的ZipFileExtensions實例,而是從 或ZipArchiveEntry的實例ZipArchive使用這些方法。
若要使用擴充方法,您必須參考專案中的 System.IO.Compression.FileSystem
元件。 元件System.IO.Compression.FileSystem
無法在 Windows 8.x 市集應用程式中使用。 因此,ZipFileExtensionsWindows 8.x 市集應用程式中無法使用元件) 中的 和 ZipFile 類別 System.IO.Compression.FileSystem
(。 在 Windows 8.x 市集應用程式中,您可以使用 、 ZipArchiveEntryDeflateStream和 GZipStream中ZipArchive的方法來處理壓縮檔案案。
類別 ZipFileExtensions 包含四個擴充 ZipArchive的方法:
類別 ZipFileExtensions 包含兩個擴充 ZipArchiveEntry的方法:
範例
下列範例示範如何從現有的檔案在 zip 封存中建立新專案,並將封存的內容解壓縮到目錄。
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string zipPath = @"c:\users\exampleuser\start.zip";
string extractPath = @"c:\users\exampleuser\extract";
string newFile = @"c:\users\exampleuser\NewFile.txt";
using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
{
archive.CreateEntryFromFile(newFile, "NewEntry.txt");
archive.ExtractToDirectory(extractPath);
}
}
}
}
Imports System.IO
Imports System.IO.Compression
Module Module1
Sub Main()
Dim zipPath As String = "c:\users\exampleuser\end.zip"
Dim extractPath As String = "c:\users\exampleuser\extract"
Dim newFile As String = "c:\users\exampleuser\NewFile.txt"
Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
archive.ExtractToDirectory(extractPath)
End Using
End Sub
End Module
下列範例示範如何逐一查看 zip 封存的內容,並擷取擴展名為 .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
方法
CreateEntryFromFile(ZipArchive, String, String) |
透過將檔案壓縮並加入至 zip 封存的方式進行封存。 |
CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) |
透過將檔案以指定之壓縮等級壓縮並加入至 zip 封存的方式進行封存。 |
ExtractToDirectory(ZipArchive, String) |
將 zip 封存中的所有檔案都解壓縮到檔案系統上的目錄。 |
ExtractToDirectory(ZipArchive, String, Boolean) |
將封存中的所有檔案解壓縮到檔案系統上目錄。 |
ExtractToFile(ZipArchiveEntry, String) |
將 zip 封存中的項目解壓縮至檔案。 |
ExtractToFile(ZipArchiveEntry, String, Boolean) |
擷取檔案 zip 封存中的項目,並選擇性地覆寫具有相同名稱的現有檔案。 |