C# ZipFile CreateFromDirectory(String, String, CompressionLevel, Boolean)

Description

ZipFile CreateFromDirectory(String, String, CompressionLevel, Boolean) Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level, and optionally includes the base directory.

Syntax

ZipFile.CreateFromDirectory(String, String, CompressionLevel, Boolean) has the following syntax.


public static void CreateFromDirectory(
  string sourceDirectoryName,//  w ww. ja va 2 s .  c o  m
  string destinationArchiveFileName,
  CompressionLevel compressionLevel,
  bool includeBaseDirectory
)

Parameters

ZipFile.CreateFromDirectory(String, String, CompressionLevel, Boolean) has the following parameters.

  • sourceDirectoryName - The path to the directory to be archived, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.
  • destinationArchiveFileName - The path of the archive to be created, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.
  • compressionLevel - One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.
  • includeBaseDirectory - true to include the directory name from sourceDirectoryName at the root of the archive; false to include only the contents of the directory.

Returns

ZipFile.CreateFromDirectory(String, String, CompressionLevel, Boolean) method returns

Example

This example shows how to create and extract a zip archive by using the ZipFile class.


using System;/*  w  ww.  ja v a2 s .  c o m*/
using System.IO;
using System.IO.Compression;

class Program
{
    static void Main(string[] args)
    {
        string startPath = @"c:\example\start";
        string zipPath = @"c:\example\result.zip";
        string extractPath = @"c:\example\extract";

        ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);

        ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
}




















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




DeflateStream
GZipStream
ZipArchive
ZipArchiveEntry
ZipFile
ZipFileExtensions