ZipFile.OpenRead(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開啟位於指定路徑的 zip 封存以讀取。
public:
static System::IO::Compression::ZipArchive ^ OpenRead(System::String ^ archiveFileName);
public static System.IO.Compression.ZipArchive OpenRead (string archiveFileName);
static member OpenRead : string -> System.IO.Compression.ZipArchive
Public Shared Function OpenRead (archiveFileName As String) As ZipArchive
參數
- archiveFileName
- String
要開啟的封存的路徑(指定為相對或絕對路徑)。 相對路徑會解譯為與目前的工作目錄相對。
傳回
已開啟的 zip 封存。
例外狀況
archiveFileName
是 Empty、只包含空白字元,或包含至少一個無效字元。
archiveFileName
為 null
。
在 archiveFileName
中,指定的路徑、檔案名稱或兩者都超過系統定義的最大長度。
archiveFileName
無效或不存在 (例如,位於未對應的磁碟機上)。
找不到在 archiveFileName
中指定的檔案。
archiveFileName
包含無效的格式。
archiveFileName
無法解譯為 zip 封存。
範例
下列範例示範如何開啟 zip 封存以供讀取。
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);
}
}
}
}
}
open System
open System.IO;
open System.IO.Compression;
[<EntryPoint>]
let main _ =
let zipPath = @".\result.zip"
printfn "Provide path where to extract the zip file:"
let extractPath = stdin.ReadLine();
// Normalizes the path.
let mutable 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) |> not then
extractPath <- extractPath + string Path.DirectorySeparatorChar
use archive = ZipFile.OpenRead zipPath
for entry in archive.Entries do
if entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) then
// Gets the full path to ensure that relative segments are removed.
let 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) then
entry.ExtractToFile destinationPath
0
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
備註
這個方法相當於呼叫 Open 方法,並將 參數設定 mode
為 Read。 封存會以 FileMode.Open 作為檔案模式值開啟。 如果封存不存在, FileNotFoundException 則會擲回例外狀況。