ZipArchive.CreateEntry ZipArchive.CreateEntry ZipArchive.CreateEntry ZipArchive.CreateEntry Method

Definition

Creates an empty entry in the zip archive.

Overloads

CreateEntry(String) CreateEntry(String) CreateEntry(String) CreateEntry(String)

Creates an empty entry that has the specified path and entry name in the zip archive.

CreateEntry(String, CompressionLevel) CreateEntry(String, CompressionLevel) CreateEntry(String, CompressionLevel)

Creates an empty entry that has the specified entry name and compression level in the zip archive.

CreateEntry(String) CreateEntry(String) CreateEntry(String) CreateEntry(String)

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

Parameters

entryName
String String String String

A path, relative to the root of the archive, that specifies the name of the entry to be created.

Returns

An empty entry in the zip archive.

Exceptions

Examples

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

Remarks

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. 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.

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. If you want to specify a different compression level, use the CreateEntry method.

CreateEntry(String, CompressionLevel) CreateEntry(String, CompressionLevel) CreateEntry(String, CompressionLevel)

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

Parameters

entryName
String String String String

A path, relative to the root of the archive, that specifies the name of the entry to be created.

compressionLevel
CompressionLevel CompressionLevel CompressionLevel CompressionLevel

One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

Returns

An empty entry in the zip archive.

Exceptions

Examples

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

Remarks

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. 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.

The value of the LastWriteTime property for the new entry is set to the current time. Set the compressionLevel parameter to Optimal if you want the file to be compressed as much as possible. Set the compressionLevel parameter to Fastest only if you are concerned that the compression operation will not complete quickly enough for your scenario.

Applies to