ZipArchive.CreateEntry 方法

定义

在 zip 存档中创建空项。Creates an empty entry in the zip archive.

重载

CreateEntry(String)

创建在 zip 存档中有指定路径和项名的空项。Creates an empty entry that has the specified path and entry name in the zip archive.

CreateEntry(String, CompressionLevel)

创建在 zip 存档中有指定项名和压缩级别的空项。Creates an empty entry that has the specified entry name and compression level in the zip archive.

CreateEntry(String)

创建在 zip 存档中有指定路径和项名的空项。Creates an empty entry that has the specified path and entry name in the zip archive.

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

相对于存档的根的路径指定了要创建项目的名称。A path, relative to the root of the archive, that specifies the name of the entry to be created.

返回

在 zip 存档中的空项。An empty entry in the zip archive.

异常

entryNameEmptyentryName is Empty.

entryNamenullentryName is null.

zip 存档不支持写入。The zip archive does not support writing.

zip 存档已释放。The zip archive has been disposed.

示例

下面的示例演示如何使用流创建项并对其进行写入。The following example shows how to create an entry and write to it by using a stream.

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

注解

entryName字符串应反映要在 zip 存档中创建的条目的相对路径。The entryName string should reflect the relative path of the entry you want to create within the zip archive. 您提供的字符串没有限制。There is no restriction on the string you provide. 但是, 如果未将其格式化为相对路径, 则会创建该条目, 但当你提取 zip 存档的内容时, 可能会出现异常。However, if it is not formatted as a relative path, the entry is created, but you may get an exception when you extract the contents of the zip archive. 如果存档中已存在具有指定路径和名称的条目, 则将创建具有相同路径和名称的第二个条目。If an entry with the specified path and name already exists in the archive, a second entry is created with the same path and name.

新项的LastWriteTime属性的值设置为当前时间。The value of the LastWriteTime property for the new entry is set to the current time. 使用基础压缩算法的默认压缩级别压缩条目。The entry is compressed using the default compression level of the underlying compression algorithm. 如果要指定不同的压缩级别, 请使用CreateEntry方法。If you want to specify a different compression level, use the CreateEntry method.

CreateEntry(String, CompressionLevel)

创建在 zip 存档中有指定项名和压缩级别的空项。Creates an empty entry that has the specified entry name and compression level in the zip archive.

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

参数

entryName
String

相对于存档的根的路径指定了要创建项目的名称。A path, relative to the root of the archive, that specifies the name of the entry to be created.

compressionLevel
CompressionLevel

指示创建项时是否强调速度或压缩有效性的枚举值之一。One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

返回

在 zip 存档中的空项。An empty entry in the zip archive.

异常

entryNameEmptyentryName is Empty.

entryNamenullentryName is null.

zip 存档不支持写入。The zip archive does not support writing.

zip 存档已释放。The zip archive has been disposed.

示例

下面的示例演示如何创建具有最佳压缩级别的条目。The following example shows how to create an entry with the optimal compression level. 它还使用流写入新条目。It also writes to the new entry by using a stream.

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("========================");
                    }
                }
            }
        }
    }
}
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字符串应反映要在 zip 存档中创建的条目的相对路径。The entryName string should reflect the relative path of the entry you want to create within the zip archive. 您提供的字符串没有限制。There is no restriction on the string you provide. 但是, 如果未将其格式化为相对路径, 则会创建该条目, 但当你提取 zip 存档的内容时, 可能会出现异常。However, if it is not formatted as a relative path, the entry is created, but you may get an exception when you extract the contents of the zip archive. 如果存档中已经存在具有指定名称的条目, 则将使用相同的名称创建另一个条目。If an entry with the specified name already exists in the archive, a second entry is created with the same name.

新项的LastWriteTime属性的值设置为当前时间。The value of the LastWriteTime property for the new entry is set to the current time. 如果希望尽可能压缩Optimal文件, 请将参数设置为。compressionLevelSet the compressionLevel parameter to Optimal if you want the file to be compressed as much as possible. 仅当你担心Fastest压缩操作不会为你的方案迅速完成时, 才将参数设置为。compressionLevelSet the compressionLevel parameter to Fastest only if you are concerned that the compression operation will not complete quickly enough for your scenario.

适用于