How to Create 7z Archive in C#

This basic article explains how to create 7z archive in C#. It includes the detailed steps and code sample to demonstrate how using C# 7z file can be created in your applications. Moreover, we do not need to install any third-party tool or compression application to work with this feature.

Steps to Create 7z Archive in C#

  1. Install Aspose.ZIP from the NuGet package manager to create 7z archive
  2. Initialize a SevenZipArchive class object
  3. Add all files and directories using the CreateEntries method
  4. Save the output archive as a 7z file

We can clearly understand the process to create a 7z archive through this step-by-step approach. The above steps also cover the configuration details and then provide the C# 7zip example code to create the archive. You can compress a single file by specifying its name or compress a whole folder by using its path.

Code to Create 7z Archive in C#

namespace Create7zArchiveInCSharp
{
class Program
{
static void Main(string[] args) // Main function to create 7z archive in CSharp
{
// Initialize a license to avoid trial version limitations in output archive
Aspose.Zip.License licForZip= new Aspose.Zip.License();
licForZip.SetLicense("Aspose.zip.lic");
// Create an empty zip archive
using (Aspose.Zip.SevenZip.SevenZipArchive archive = new Aspose.Zip.SevenZip.SevenZipArchive())
{
// Call the CreateEntries function to add the folder containing the contents
archive.CreateEntries("folder");
// Save the archive as 7z
archive.Save("folder.7z");
}
System.Console.WriteLine("Done");
}
}
}

For creating an archive of type 7z C# environment details and sample code is shared here. This code takes the path of a folder and creates a 7zip archive file containing all the contents of the folder. Moreover, you can enhance this code to incorporate AES encryption and password protection for the archive. Note that you can scale this code by embedding this into a thread-based application while taking the benefit of parallel processing.

This tutorial explains the details for working with 7zip C#-based application. However, if you want to learn to extract files from a ZIP file, you can read the article on how to extract ZIP file in C#.

 English