C# ZipFileExtensions CreateEntryFromFile(ZipArchive, String, String)

Description

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

Syntax

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


public static ZipArchiveEntry CreateEntryFromFile(
  this ZipArchive destination,
  string sourceFileName,/*from  www .j  a  v  a 2s . c o m*/
  string entryName
)

Parameters

ZipFileExtensions.CreateEntryFromFile(ZipArchive, String, String) 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.

Returns

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

Example

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


//  w w  w .  j  a  v a 2  s.  c  o  m
using System;
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");
            archive.ExtractToDirectory(extractPath);
        } 
    }
}




















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




DeflateStream
GZipStream
ZipArchive
ZipArchiveEntry
ZipFile
ZipFileExtensions