Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ZipArchive.CreateEntry Method (String)

.NET Framework 4.6 and 4.5

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

Namespace:  System.IO.Compression
Assembly:  System.IO.Compression (in System.IO.Compression.dll)

public ZipArchiveEntry CreateEntry(
	string entryName
)

Parameters

entryName
Type: System.String

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

Return Value

Type: System.IO.Compression.ZipArchiveEntry
An empty entry in the zip archive.

ExceptionCondition
ArgumentException

entryName is Empty.

ArgumentNullException

entryName is null.

NotSupportedException

The zip archive does not support writing.

ObjectDisposedException

The zip archive has been disposed.

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.

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("========================");
                    }
                }
            }
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5

.NET for Windows Phone apps

Supported in: Windows Phone 8.1
Show:
© 2015 Microsoft