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.ZIPand 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:
- Create a FileStream: Open a new
FileStreaminstance to write the 7z file. - Initialize SevenZipArchive Class: Use the
SevenZipArchiveclass to start working with the 7z format. - Add File Entry: Use the
CreateEntrymethod of theSevenZipArchiveclass to add a single entry (file) into the archive. - Save Archive: Finally, save the created archive using the
Savemethod.
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:
- Create SevenZipArchive Class: Initialize the
SevenZipArchiveclass. - Add Multiple Entries: Use the
CreateEntriesmethod of theSevenZipArchiveclass to add multiple entries by providing the folder path. - Save Archive: Save the created archive using the
Savemethod.
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:
- Create SevenZipAESEncryptionSettings: Initialize the
SevenZipAESEncryptionSettingsclass to set up the necessary encryption parameters. - 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:
- Create FileStream: Open a new
FileStreaminstance to write the 7z file. - Add Entries with Individual Passwords: Use the
CreateEntrymethod of theSevenZipArchiveclass, specifying individual passwords for each entry. - 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.