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

Description

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

Syntax

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


public static void CreateFromDirectory(
  string sourceDirectoryName,// w w w. jav  a  2s.co  m
  string destinationArchiveFileName,
  CompressionLevel compressionLevel,
  bool includeBaseDirectory,
  Encoding entryNameEncoding
)

Parameters

ZipFile.CreateFromDirectory(String, String, CompressionLevel, Boolean, Encoding) 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.
  • entryNameEncoding - The encoding to use when reading or writing entry names in this archive. Specify a value for this parameter only when an encoding is required for interoperability with zip archive tools and libraries that do not support UTF-8 encoding for entry names.

Returns

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

Example


// ww w  .  java2 s . c o  m
using System;
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,UTF8);

        ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
}




















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




DeflateStream
GZipStream
ZipArchive
ZipArchiveEntry
ZipFile
ZipFileExtensions