ZipArchive.CreateEntry Method (String, CompressionLevel)

.NET Framework (current version)
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

Public Function CreateEntry (
	entryName As String,
	compressionLevel As CompressionLevel
) As ZipArchiveEntry

Parameters

entryName
Type: System.String

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

compressionLevel
Type: System.IO.Compression.CompressionLevel

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

Return Value

Type: System.IO.Compression.ZipArchiveEntry

An empty entry in the zip archive.

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

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.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top
Show: