FileInfo - OpenZipFile

Opens a zip archive at the specified path and in the specified mode.

Try it

public static void Main()
{
    CreateZipFile();

    var dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExtractZipFileToDirectory"));
    if (dir.Exists)
    {
        dir.Clear();
    }
    var zip = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZipFileDirectory", "test.zip"));

    Directory.CreateDirectory(zip.Directory.FullName);

	//C# Extension Method: FileInfo - OpenZipFile
    using (ZipArchive archive = zip.OpenZipFile(ZipArchiveMode.Update))
    {
        archive.CreateEntry("NewEntry.txt");
    }

    string extractPath = dir.FullName;

    if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
        extractPath += Path.DirectorySeparatorChar;

    using (var archive = zip.OpenReadZipFile())
    {
        foreach (ZipArchiveEntry entry in archive.Entries)
        {
            Console.WriteLine(entry.FullName);
        }
    }
}
public static void CreateZipFile()
{
    var dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TxtFileDirectory"));
    var dir2 = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZipFileDirectory"));

    if (dir.Exists)
    {
        dir.Clear();
    }
    if (dir2.Exists)
    {
        dir2.Clear();
    }

    // Type
    var @this = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TxtFileDirectory", "test.txt"));
    var zip = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZipFileDirectory", "test.zip"));

    Directory.CreateDirectory(@this.Directory.FullName);
    Directory.CreateDirectory(zip.Directory.FullName);

    // Intialization
    using (FileStream stream = @this.Create())
    {
    }
    @this.Directory.CreateZipFile(zip);
}

View Source
#if NET45_OR_GREATER
using System.IO;
using System.IO.Compression;
using System.Text;

public static partial class Extensions
{
    /// <summary>Opens a zip archive at the specified path and in the specified mode.</summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="mode">
    ///     One of the enumeration values that specifies the actions that are allowed
    ///     on the entries in the opened archive.
    /// </param>
    /// <returns>A ZipArchive.</returns>
    public static ZipArchive OpenZipFile(this FileInfo @this, ZipArchiveMode mode)
    {
        return ZipFile.Open(@this.FullName, mode);
    }

    /// <summary>Opens a zip archive at the specified path and in the specified mode.</summary>
    /// <param name="this">
    ///     The path to the archive to open, specified as a relative or absolute
    ///     path. A relative path is interpreted as relative to the current working directory.
    /// </param>
    /// <param name="mode">
    ///     One of the enumeration values that specifies the actions that are allowed
    ///     on the entries in the opened archive.
    /// </param>
    /// <param name="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.
    /// </param>
    /// <returns>A ZipArchive.</returns>
    public static ZipArchive OpenZipFile(this FileInfo @this, ZipArchiveMode mode, Encoding entryNameEncoding)
    {
        return ZipFile.Open(@this.FullName, mode, entryNameEncoding);
    }
}
#endif