Creating 7z Archives Using C#

In this tutorial, you will learn how to create 7z (7-Zip) archives using C# programming language. We will explore different scenarios where a single file and multiple files can be compressed into a 7z archive. Additionally, we will cover how to encrypt these archives with AES encryption for enhanced security.

Using Aspose.ZIP for .NET API

To get started, you need to install the Aspose.ZIP for .NET library. You can install it via NuGet Package Manager or download the DLL and include it in your project.

Installation via NuGet

  • Open Visual Studio.
  • Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
  • Search for Aspose.ZIP and install the package.

Downloading the DLL Manually

  • Visit the downloads page of Aspose.
  • Download the required version of the Aspose.Zip.dll file and add it to your project references.

Creating a 7z Archive with Single Input

To create a 7z archive from a single file, follow these steps:

  1. Create a FileStream: Open a new FileStream instance to write the 7z file.
  2. Initialize SevenZipArchive Class: Use the SevenZipArchive class to start working with the 7z format.
  3. Add File Entry: Use the CreateEntry method of the SevenZipArchive class to add a single entry (file) into the archive.
  4. Save Archive: Finally, save the created archive using the Save method.

Example Code

using Aspose.Zip;
using Aspose.Zip.SevenZip;

// Step 1: Create FileStream
FileStream fileStream = new FileStream("output.7z", FileMode.Create);

// Step 2: Initialize SevenZipArchive
SevenZipArchive sevenZipArchive = new SevenZipArchive(fileStream, SevenZipArchiveMode.Create);

// Step 3: Add File Entry
sevenZipArchive.CreateEntry("inputFile.txt");

// Step 4: Save Archive
sevenZipArchive.Save();

Creating a 7z Archive with Multiple Inputs

To compress multiple files into a single 7z archive, follow these steps:

  1. Create SevenZipArchive Class: Initialize the SevenZipArchive class.
  2. Add Multiple Entries: Use the CreateEntries method of the SevenZipArchive class to add multiple entries by providing the folder path.
  3. Save Archive: Save the created archive using the Save method.

Example Code

using Aspose.Zip;
using Aspose.Zip.SevenZip;

// Step 1: Create SevenZipArchive
SevenZipArchive sevenZipArchive = new SevenZipArchive("output.7z", SevenZipArchiveMode.Create);

// Step 2: Add Multiple Entries
sevenZipArchive.CreateEntries(@"C:\path\to\folder");

// Step 3: Save Archive
sevenZipArchive.Save();

Creating an AES-Encrypted 7z Archive

For enhanced security, you can use AES encryption when creating a 7z archive. Here’s how to do it:

  1. Create SevenZipAESEncryptionSettings: Initialize the SevenZipAESEncryptionSettings class to set up the necessary encryption parameters.
  2. Add Input and Save Archive: Add your input files to the archive, apply the encryption settings, and save the archive.

Example Code

using Aspose.Zip;
using Aspose.Zip.Saving;

// Step 1: Create SevenZipAESEncryptionSettings
SevenZipAESEncryptionSettings aesEncryptionSettings = new SevenZipAESEncryptionSettings();
aesEncryptionSettings.Password = "yourpassword"; // Set your password here

// Step 2: Add Input and Save Archive
using (FileStream fileStream = new FileStream("output.7z", FileMode.Create))
{
    SevenZipArchive sevenZipArchive = new SevenZipArchive(fileStream, SevenZipArchiveMode.Create);
    sevenZipArchive.CreateEntry("inputFile.txt");
    sevenZipArchive.Save(aesEncryptionSettings);
}

Setting Different Passwords for 7z Inputs

To assign unique passwords to different entries in your archive:

  1. Create FileStream: Open a new FileStream instance to write the 7z file.
  2. Add Entries with Individual Passwords: Use the CreateEntry method of the SevenZipArchive class, specifying individual passwords for each entry.
  3. Save Archive: Save the created archive.

Example Code

using Aspose.Zip;
using Aspose.Zip.SevenZip;

// Step 1: Create FileStream
FileStream fileStream = new FileStream("output.7z", FileMode.Create);

// Step 2: Add Entries with Individual Passwords
SevenZipArchive sevenZipArchive = new SevenZipArchive(fileStream, SevenZipArchiveMode.Create);
sevenZipArchive.CreateEntry("inputFile1.txt", "password1");
sevenZipArchive.CreateEntry("inputFile2.txt", "password2");

// Step 3: Save Archive
sevenZipArchive.Save();

Conclusion

In this article, we explored how to create 7z archives using C#, ensure their security with AES encryption, and assign unique passwords to different inputs. With the Aspose Plugin for .NET, you can effectively manage files, apply encryption, and streamline your workflow—all at a cost of just $99.

By leveraging Aspose.ZIP for .NET, developers can handle various compression tasks efficiently while maintaining data security. This tool is particularly useful for scenarios requiring robust file handling and secure archiving.

Whether you’re dealing with single or multiple files, understanding these techniques will help you create more efficient and secure 7z archives in your C# projects.

More in this category