語言

ZipArchive.CreateEntry 方法

定義

在壓縮壓縮檔中建立一個空項目。

多載

名稱 Description
CreateEntry(String, CompressionLevel, ReadOnlySpan<Char>, ZipEncryptionMethod)
CreateEntry(String, ReadOnlySpan<Char>, ZipEncryptionMethod)
CreateEntry(String)

在壓縮壓縮檔中建立一個空的條目,該條目包含指定的路徑和條目名稱。

CreateEntry(String, CompressionLevel)

在壓縮壓縮檔中建立一個空項目,該項目名稱與壓縮等級相同。

CreateEntry(String, CompressionLevel, ReadOnlySpan<Char>, ZipEncryptionMethod)

來源:
ZipArchive.cs
public:
 System::IO::Compression::ZipArchiveEntry ^ CreateEntry(System::String ^ entryName, System::IO::Compression::CompressionLevel compressionLevel, ReadOnlySpan<char> password, System::IO::Compression::ZipEncryptionMethod encryptionMethod);
public System.IO.Compression.ZipArchiveEntry CreateEntry(string entryName, System.IO.Compression.CompressionLevel compressionLevel, ReadOnlySpan<char> password, System.IO.Compression.ZipEncryptionMethod encryptionMethod);
member this.CreateEntry : string * System.IO.Compression.CompressionLevel * ReadOnlySpan<char> * System.IO.Compression.ZipEncryptionMethod -> System.IO.Compression.ZipArchiveEntry
Public Function CreateEntry (entryName As String, compressionLevel As CompressionLevel, password As ReadOnlySpan(Of Char), encryptionMethod As ZipEncryptionMethod) As ZipArchiveEntry

參數

entryName
String
compressionLevel
CompressionLevel
password
ReadOnlySpan<Char>
encryptionMethod
ZipEncryptionMethod

傳回

適用於

CreateEntry(String, ReadOnlySpan<Char>, ZipEncryptionMethod)

來源:
ZipArchive.cs
public:
 System::IO::Compression::ZipArchiveEntry ^ CreateEntry(System::String ^ entryName, ReadOnlySpan<char> password, System::IO::Compression::ZipEncryptionMethod encryptionMethod);
public System.IO.Compression.ZipArchiveEntry CreateEntry(string entryName, ReadOnlySpan<char> password, System.IO.Compression.ZipEncryptionMethod encryptionMethod);
member this.CreateEntry : string * ReadOnlySpan<char> * System.IO.Compression.ZipEncryptionMethod -> System.IO.Compression.ZipArchiveEntry
Public Function CreateEntry (entryName As String, password As ReadOnlySpan(Of Char), encryptionMethod As ZipEncryptionMethod) As ZipArchiveEntry

參數

entryName
String
password
ReadOnlySpan<Char>
encryptionMethod
ZipEncryptionMethod

傳回

適用於

CreateEntry(String)

來源:
ZipArchive.cs
來源:
ZipArchive.cs
來源:
ZipArchive.cs
來源:
ZipArchive.cs
來源:
ZipArchive.cs

在壓縮壓縮檔中建立一個空的條目,該條目包含指定的路徑和條目名稱。

public:
 System::IO::Compression::ZipArchiveEntry ^ CreateEntry(System::String ^ entryName);
public System.IO.Compression.ZipArchiveEntry CreateEntry(string entryName);
member this.CreateEntry : string -> System.IO.Compression.ZipArchiveEntry
Public Function CreateEntry (entryName As String) As ZipArchiveEntry

參數

entryName
String

相對於歸檔根的路徑,指定要建立的條目名稱。

傳回

壓縮檔中的一個空白條目。

例外狀況

entryName 是 Empty。

entryName 是 null。

zip 壓縮檔不支援書寫。

壓縮壓縮檔已被處理。

範例

以下範例展示了如何利用串流建立條目並寫入該條目。

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("========================");
                    }
                }
            }
        }
    }
}
open System.IO
open System.IO.Compression

do
    use zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open)

    use archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)

    let readmeEntry = archive.CreateEntry "Readme.txt"
    use 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

備註

字 entryName 串應該反映你想在壓縮壓縮檔中建立條目的相對路徑。 你提供的弦線沒有限制。 不過,如果格式不是相對路徑,條目會被建立,但當你解壓壓縮壓縮檔內容時,可能會遇到例外。 如果壓縮檔中已有具有指定路徑與名稱的條目,則會建立第二條相同路徑與名稱的條目。

新條目的財產價值 LastWriteTime 會設定為當前時間。 該條目會使用底層壓縮演算法的預設壓縮等級進行壓縮。 如果你想指定不同的壓縮等級,就用這個 CreateEntry 方法。

適用於

CreateEntry(String, CompressionLevel)

來源:
ZipArchive.cs
來源:
ZipArchive.cs
來源:
ZipArchive.cs
來源:
ZipArchive.cs
來源:
ZipArchive.cs

在壓縮壓縮檔中建立一個空項目,該項目名稱與壓縮等級相同。

public:
 System::IO::Compression::ZipArchiveEntry ^ CreateEntry(System::String ^ entryName, System::IO::Compression::CompressionLevel compressionLevel);
public System.IO.Compression.ZipArchiveEntry CreateEntry(string entryName, System.IO.Compression.CompressionLevel compressionLevel);
member this.CreateEntry : string * System.IO.Compression.CompressionLevel -> System.IO.Compression.ZipArchiveEntry
Public Function CreateEntry (entryName As String, compressionLevel As CompressionLevel) As ZipArchiveEntry

參數

entryName
String

相對於歸檔根的路徑,指定要建立的條目名稱。

compressionLevel
CompressionLevel

其中一個列舉值表示在建立條目時,應該強調速度還是壓縮效能。

傳回

壓縮檔中的一個空白條目。

例外狀況

entryName 是 Empty。

entryName 是 null。

zip 壓縮檔不支援書寫。

壓縮壓縮檔已被處理。

範例

以下範例展示了如何建立最佳壓縮水平的輸入。 它也會透過串流寫入新條目。

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", CompressionLevel.Optimal);
                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine("Information about this package.");
                            writer.WriteLine("========================");
                    }
                }
            }
        }
    }
}
open System.IO
open System.IO.Compression

do
    use zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open)

    use archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)

    let readmeEntry = archive.CreateEntry("Readme.txt", CompressionLevel.Optimal)
    use 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", CompressionLevel.Optimal)
                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

備註

字 entryName 串應該反映你想在壓縮壓縮檔中建立條目的相對路徑。 你提供的弦線沒有限制。 不過,如果格式不是相對路徑,條目會被建立,但當你解壓壓縮壓縮檔內容時,可能會遇到例外。 如果檔案中已有指定名稱的條目,則會建立第二個同名條目。

新條目的財產價值 LastWriteTime 會設定為當前時間。 如果你想讓檔案盡可能壓縮,就compressionLevel設定Optimal參數為。 只有在你擔心壓縮操作無法足夠快完成時,才將參數設compressionLevel為Fastest

適用於