ZipFileExtensions.CreateEntryFromFile 方法

定義

透過將檔案壓縮並加入至 zip 封存的方式進行封存。

多載

CreateEntryFromFile(ZipArchive, String, String)

透過將檔案壓縮並加入至 zip 封存的方式進行封存。

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

透過將檔案以指定之壓縮等級壓縮並加入至 zip 封存的方式進行封存。

CreateEntryFromFile(ZipArchive, String, String)

來源:
ZipFileExtensions.ZipArchive.Create.cs
來源:
ZipFileExtensions.ZipArchive.Create.cs
來源:
ZipFileExtensions.ZipArchive.Create.cs

透過將檔案壓縮並加入至 zip 封存的方式進行封存。

public:
[System::Runtime::CompilerServices::Extension]
 static System::IO::Compression::ZipArchiveEntry ^ CreateEntryFromFile(System::IO::Compression::ZipArchive ^ destination, System::String ^ sourceFileName, System::String ^ entryName);
public static System.IO.Compression.ZipArchiveEntry CreateEntryFromFile (this System.IO.Compression.ZipArchive destination, string sourceFileName, string entryName);
static member CreateEntryFromFile : System.IO.Compression.ZipArchive * string * string -> System.IO.Compression.ZipArchiveEntry
<Extension()>
Public Function CreateEntryFromFile (destination As ZipArchive, sourceFileName As String, entryName As String) As ZipArchiveEntry

參數

destination
ZipArchive

要將檔案加入至其中的 Zip 封存。

sourceFileName
String

要封存的檔案之路徑。 您可以指定相對或絕對路徑。 相對路徑會解譯為與目前的工作目錄相對。

entryName
String

要在 Zip 封存中建立之項目的名稱。

傳回

zip 封存中之新項目的包裝函式。

例外狀況

sourceFileNameEmpty、只包含空白字元,或包含至少一個無效字元。

-或-

entryNameEmpty

sourceFileNameentryNamenull

sourceFileName 中,指定的路徑、檔案名稱或兩者都超過系統定義的最大長度。

sourceFileName 無效 (例如它位於未對應的磁碟機上)。

指定的 sourceFileName 檔案無法開啟,或太大而無法更新, (目前的限制為 Int32.MaxValue) 。

sourceFileName 會指定目錄。

-或-

呼叫端沒有所需的使用權限來存取 sourceFileName 指定的檔案。

找不到 sourceFileName 所指定的檔案。

sourceFileName 參數的格式無效。

-或-

Zip 封存不支援寫入。

Zip 封存已經處置。

範例

下列範例示範如何從現有的檔案在 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

備註

封存中的新專案包含 所 sourceFileName 指定的檔案內容。 如果具有指定名稱的專案 (entryName) 已存在於封存中,則會使用相同的名稱建立第二個專案。 專案的 LastWriteTime 屬性會設定為上次變更檔案系統上的檔案時。

出現時 ZipArchiveMode.Update ,專案的大小限制會限制為 Int32.MaxValue 。 此限制是因為更新模式會在內部使用 MemoryStream 來允許更新封存時所需的搜尋,且 MemoryStream 最大等於 int 的大小。

適用於

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

來源:
ZipFileExtensions.ZipArchive.Create.cs
來源:
ZipFileExtensions.ZipArchive.Create.cs
來源:
ZipFileExtensions.ZipArchive.Create.cs

透過將檔案以指定之壓縮等級壓縮並加入至 zip 封存的方式進行封存。

public:
[System::Runtime::CompilerServices::Extension]
 static System::IO::Compression::ZipArchiveEntry ^ CreateEntryFromFile(System::IO::Compression::ZipArchive ^ destination, System::String ^ sourceFileName, System::String ^ entryName, System::IO::Compression::CompressionLevel compressionLevel);
public static System.IO.Compression.ZipArchiveEntry CreateEntryFromFile (this System.IO.Compression.ZipArchive destination, string sourceFileName, string entryName, System.IO.Compression.CompressionLevel compressionLevel);
static member CreateEntryFromFile : System.IO.Compression.ZipArchive * string * string * System.IO.Compression.CompressionLevel -> System.IO.Compression.ZipArchiveEntry
<Extension()>
Public Function CreateEntryFromFile (destination As ZipArchive, sourceFileName As String, entryName As String, compressionLevel As CompressionLevel) As ZipArchiveEntry

參數

destination
ZipArchive

要將檔案加入至其中的 Zip 封存。

sourceFileName
String

要封存的檔案之路徑。 您可以指定相對或絕對路徑。 相對路徑會解譯為與目前的工作目錄相對。

entryName
String

要在 Zip 封存中建立之項目的名稱。

compressionLevel
CompressionLevel

其中一個列舉值,指出當建立項目時是否要強調速度或壓縮的效益。

傳回

zip 封存中之新項目的包裝函式。

例外狀況

sourceFileNameEmpty、只包含空白字元,或包含至少一個無效字元。

-或-

entryNameEmpty

sourceFileNameentryNamenull

sourceFileName 無效 (例如它位於未對應的磁碟機上)。

sourceFileName 中,指定的路徑、檔案名稱或兩者都超過系統定義的最大長度。

指定的 sourceFileName 檔案無法開啟,或太大而無法更新, (目前的限制為 Int32.MaxValue) 。

sourceFileName 會指定目錄。

-或-

呼叫端沒有所需的使用權限來存取 sourceFileName 指定的檔案。

找不到 sourceFileName 所指定的檔案。

sourceFileName 參數的格式無效。

-或-

Zip 封存不支援寫入。

Zip 封存已經處置。

範例

下列範例示範如何從現有的檔案在 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", CompressionLevel.Fastest);
                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

備註

封存中的新專案包含 所 sourceFileName 指定的檔案內容。 如果具有指定名稱的專案 (entryName) 已存在於封存中,則會使用相同的名稱建立第二個專案。 專案的 LastWriteTime 屬性會設定為上次變更檔案系統上的檔案時。

出現時 ZipArchiveMode.Update ,專案的大小限制會限制為 Int32.MaxValue 。 此限制是因為更新模式會在內部使用 MemoryStream 來允許更新封存時所需的搜尋,且 MemoryStream 最大等於 int 的大小。

適用於