# ZipFileExtensions.CreateEntryFromFile Method (ZipArchive, String, String, CompressionLevel)

.NET Framework 4.5

Archives a file by compressing it using the specified compression level and adding it to the zip archive.

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

## Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function CreateEntryFromFile ( _
destination As ZipArchive, _
sourceFileName As String, _
entryName As String, _
compressionLevel As CompressionLevel _
) As ZipArchiveEntry


#### Parameters

destination
Type: System.IO.Compression.ZipArchive

The zip archive to add the file to.

sourceFileName
Type: System.String

The path to the file to be archived. You can specify either a relative or an absolute path. A relative path is interpreted as relative to the current working directory.

entryName
Type: System.String

The name of the entry to create in the zip archive.

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
A wrapper for the new entry in the zip archive.

#### Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type ZipArchive. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

## Exceptions

ExceptionCondition
ArgumentException

sourceFileName is Empty, contains only white space, or contains at least one invalid character.

-or-

entryName is Empty.

ArgumentNullException

sourceFileName or entryName is Nothing.

DirectoryNotFoundException

sourceFileName is invalid (for example, it is on an unmapped drive).

PathTooLongException

In sourceFileName, the specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must not exceed 248 characters, and file names must not exceed 260 characters.

IOException

The file specified by sourceFileName cannot be opened.

UnauthorizedAccessException

sourceFileName specifies a directory.

-or-

The caller does not have the required permission to access the file specified by sourceFileName.

FileNotFoundException

NotSupportedException

The sourceFileName parameter is in an invalid format.

-or-

The zip archive does not support writing.

ObjectDisposedException

The zip archive has been disposed.

## Remarks

The new entry in the archive contains the contents of the file specified by sourceFileName. If an entry with the specified name (entryName) already exists in the archive, a second entry is created with an identical name. The LastWriteTime property of the entry is set to the last time the file on the file system was changed.

## Examples

The following example shows how to create a new entry in a zip archive from an existing file, and specify the compression level.

Imports System.IO
Imports System.IO.Compression

Module Module1

Sub Main()
Dim zipPath As String = "c:\users\exampleuser\end.zip"
Dim extractPath As String = "c:\users\exampleuser\extract"
Dim newFile As String = "c:\users\exampleuser\NewFile.txt"

Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
archive.ExtractToDirectory(extractPath)
End Using
End Sub

End Module


## Version Information

#### .NET Framework

Supported in: 4.5.2, 4.5.1, 4.5

## Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.