
In a previous post, we explored how to create ZIP archives using C#. Now, we’ll shift our focus to the 7z format, which is renowned for its high compression ratio and robust 256-bit AES encryption capabilities. This makes the .NET 7z library an ideal choice for handling archives securely and efficiently.
What You Will Learn
In this tutorial, you’ll learn how to create 7z (7-Zip) archives programmatically using C#. We will cover the following topics:
- Creating a 7z Archive with a Single Entry
- Creating an AES Encrypted 7z Archive
- Setting Different Passwords for 7z Entries
We’ll be using the Aspose.ZIP for .NET API. You can install this library via:
- NuGet Package Manager
- Download the DLL and include it in your project
Create 7z (7-Zip) Archive using C#
You can compress files into a 7z archive, suitable for both single file and multiple files scenarios. This functionality provides a cost-effective solution for developers looking for a .NET RAR extraction tool.
Create 7z Archive with a Single Entry
To create a 7z archive with a single file, follow these steps:
- Open a new FileStream to create the 7z file.
- Instantiate the SevenZipArchive class.
- Add the file using SevenZipArchive.CreateEntry.
- Save the archive with SevenZipArchive.Save.
Here’s a C# example to illustrate:
Create 7z Archive with Multiple Entries
To compress multiple files into a single 7z archive:
- Create an instance of the SevenZipArchive class.
- Use SevenZipArchive.CreateEntries and pass the folder path.
- Save the archive with SevenZipArchive.Save.
Example:
Create an AES Encrypted 7z Archive using C#
To secure your archives, you can utilize AES encryption, a feature of the .NET 7z library that enhances archive security:
- Create an instance of SevenZipAESEncryptionSettings.
- Configure the necessary encryption parameters.
- Add entries to the archive and save it.
Example:
Set Different Passwords for 7z Entries using C#
To assign unique passwords to different entries within your archive:
- Open a FileStream for the 7z archive.
- Use SevenZipArchive.CreateEntry to add entries while specifying individual passwords.
- Save the archive.
Example:
Conclusion
In this article, we have explored how to create 7z archives programmatically in C#, secure them with AES encryption, and assign unique passwords to entries. For more advanced features and best practices, check out the Aspose.ZIP documentation.
By utilizing the Aspose Plugin for .NET, you can efficiently manage archives, apply encryption, and streamline your workflows—all for just $99. Unlock advanced features today with this reliable .NET archive extraction plugin, supporting all major formats!