Create 7Zip Archive in C#

In a previous post, we discussed creating ZIP archives using C#. Now, let’s focus on the 7z format, known for its high compression ratio and support for 256-bit AES encryption, which secures files and their names. This makes the .NET 7z library an excellent choice for robust and reliable archive handling.

What You Will Learn

In this guide, you’ll learn how to create 7z (7-Zip) archives programmatically using C#. We’ll cover:

Examples use the Aspose.ZIP for .NET API. Install the API using:

Create 7z (7-Zip) Archive using C#

You can compress files into a 7z archive for both single file and multiple files scenarios, making it a low-cost .NET RAR extraction tool for budget-conscious developers.

Create 7z Archive with a Single Entry

Steps to create a 7z archive with a single file:

  1. Open a new FileStream to create the 7z file.
  2. Create an instance of the SevenZipArchive class.
  3. Add the file using SevenZipArchive.CreateEntry.
  4. Save the archive using SevenZipArchive.Save.

Here’s a C# example:

Create 7z Archive with Multiple Entries

To compress multiple files:

  1. Create an instance of the SevenZipArchive class.
  2. Pass the folder path to SevenZipArchive.CreateEntries.
  3. Save the archive using SevenZipArchive.Save.

Example:

Create an AES Encrypted 7z Archive using C#

Use AES encryption for secure archives, which is a feature of the .NET 7z library for advanced archive features:

  1. Create an instance of SevenZipAESEncryptionSettings.
  2. Configure encryption parameters.
  3. Add entries to the archive and save it.

Example:

Set Different Passwords for 7z Entries using C#

To assign unique passwords:

  1. Open a FileStream for the 7z archive.
  2. Add entries using SevenZipArchive.CreateEntry and specify individual passwords.
  3. Save the archive.

Example:

Conclusion

In this article, we’ve covered how to create 7z archives programmatically in C#, secure them with AES encryption, and assign unique passwords to entries. Explore more features with the Aspose.ZIP documentation and discover the best practices for nested zip extraction in .NET.

By using the Aspose Plugin for .NET, you can easily manage archives, apply encryption, and streamline your workflows for just $99. Unlock advanced features today with the reliable .NET archive extraction plugin supporting all major formats!