Edit

Share via


ZipFileExtensions.CreateEntryFromFile Method

Definition

Archives a file by compressing it and adding it to the zip archive.

Overloads

CreateEntryFromFile(ZipArchive, String, String)

Archives a file by compressing it and adding it to the zip archive.

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

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

CreateEntryFromFile(ZipArchive, String, String)

Source:
ZipFileExtensions.ZipArchive.Create.cs
Source:
ZipFileExtensions.ZipArchive.Create.cs
Source:
ZipFileExtensions.ZipArchive.Create.cs

Archives a file by compressing it and adding it to the zip archive.

public:
[System::Runtime::CompilerServices::Extension]
 static System::IO::Compression::ZipArchiveEntry ^ CreateEntryFromFile(System::IO::Compression::ZipArchive ^ destination, System::String ^ sourceFileName, System::String ^ entryName);
public static System.IO.Compression.ZipArchiveEntry CreateEntryFromFile (this System.IO.Compression.ZipArchive destination, string sourceFileName, string entryName);
static member CreateEntryFromFile : System.IO.Compression.ZipArchive * string * string -> System.IO.Compression.ZipArchiveEntry
<Extension()>
Public Function CreateEntryFromFile (destination As ZipArchive, sourceFileName As String, entryName As String) As ZipArchiveEntry

Parameters

destination
ZipArchive

The zip archive to add the file to.

sourceFileName
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
String

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

Returns

A wrapper for the new entry in the zip archive.

Exceptions

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

-or-

entryName is Empty.

sourceFileName or entryName is null.

In sourceFileName, the specified path, file name, or both exceed the system-defined maximum length.

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

The file specified by sourceFileName cannot be opened, or is too large to be updated (current limit is Int32.MaxValue.

sourceFileName specifies a directory.

-or-

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

The file specified by sourceFileName is not found.

The sourceFileName parameter is in an invalid format.

-or-

The zip archive does not support writing.

The zip archive has been disposed.

Examples

The following example shows how to create a new entry in a zip archive from an existing file.

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\users\exampleuser\start.zip";
            string extractPath = @"c:\users\exampleuser\extract";
            string newFile = @"c:\users\exampleuser\NewFile.txt";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(newFile, "NewEntry.txt");
                archive.ExtractToDirectory(extractPath);
            }
        }
    }
}
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

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.

When ZipArchiveMode.Update is present, the size limit of an entry is limited to Int32.MaxValue. This limit is because update mode uses a MemoryStream internally to allow the seeking required when updating an archive, and MemoryStream has a maximum equal to the size of an int.

Applies to

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

Source:
ZipFileExtensions.ZipArchive.Create.cs
Source:
ZipFileExtensions.ZipArchive.Create.cs
Source:
ZipFileExtensions.ZipArchive.Create.cs

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

public:
[System::Runtime::CompilerServices::Extension]
 static System::IO::Compression::ZipArchiveEntry ^ CreateEntryFromFile(System::IO::Compression::ZipArchive ^ destination, System::String ^ sourceFileName, System::String ^ entryName, System::IO::Compression::CompressionLevel compressionLevel);
public static System.IO.Compression.ZipArchiveEntry CreateEntryFromFile (this System.IO.Compression.ZipArchive destination, string sourceFileName, string entryName, System.IO.Compression.CompressionLevel compressionLevel);
static member CreateEntryFromFile : System.IO.Compression.ZipArchive * string * string * System.IO.Compression.CompressionLevel -> System.IO.Compression.ZipArchiveEntry
<Extension()>
Public Function CreateEntryFromFile (destination As ZipArchive, sourceFileName As String, entryName As String, compressionLevel As CompressionLevel) As ZipArchiveEntry

Parameters

destination
ZipArchive

The zip archive to add the file to.

sourceFileName
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
String

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

compressionLevel
CompressionLevel

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

Returns

A wrapper for the new entry in the zip archive.

Exceptions

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

-or-

entryName is Empty.

sourceFileName or entryName is null.

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

In sourceFileName, the specified path, file name, or both exceed the system-defined maximum length.

The file specified by sourceFileName cannot be opened, or is too large to be updated (current limit is Int32.MaxValue.

sourceFileName specifies a directory.

-or-

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

The file specified by sourceFileName is not found.

The sourceFileName parameter is in an invalid format.

-or-

The zip archive does not support writing.

The zip archive has been disposed.

Examples

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

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\users\exampleuser\start.zip";
            string extractPath = @"c:\users\exampleuser\extract";
            string newFile = @"c:\users\exampleuser\NewFile.txt";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest);
                archive.ExtractToDirectory(extractPath);
            }
        }
    }
}
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

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.

When ZipArchiveMode.Update is present, the size limit of an entry is limited to Int32.MaxValue. This limit is because update mode uses a MemoryStream internally to allow the seeking required when updating an archive, and MemoryStream has a maximum equal to the size of an int.

Applies to