C# ZipFileExtensions CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

Description

ZipFileExtensions CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) Archives a file by compressing it using the specified compression level and adding it to the zip archive.

Syntax

ZipFileExtensions.CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) has the following syntax.


public static ZipArchiveEntry CreateEntryFromFile(
  this ZipArchive destination,
  string sourceFileName,/*from ww w  . j a  va 2s .c  o m*/
  string entryName,
  CompressionLevel compressionLevel
)

Parameters

ZipFileExtensions.CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) has the following parameters.

  • destination - The zip archive to add the file to.
  • sourceFileName - 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 - The name of the entry to create in the zip archive.
  • compressionLevel - One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

Returns

ZipFileExtensions.CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) method returns A wrapper for the new entry in the zip archive.

Example


using System;/*from ww  w .  j  a v  a2s . co  m*/
using System.IO;
using System.IO.Compression;

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);
        } 
    }
}




















Home »
  C# Tutorial »
    System.IO.Compression »




DeflateStream
GZipStream
ZipArchive
ZipArchiveEntry
ZipFile
ZipFileExtensions