In a previous post, we explored various methods of creating ZIP archives using C#. This article will expand on that knowledge by focusing on the 7z format, which is based on an open-source architecture and offers a high compression ratio. Notably, 7z supports 256-bit AES encryption, allowing you to secure not just the files but also their names.
In this guide, you’ll learn how to create 7z (7-Zip) archives programmatically using C#. We will cover:
- Creating a 7z Archive with a Single Entry
- Creating an AES Encrypted 7z Archive
- Setting Different Passwords for 7z Entries
All examples and code snippets are based on the Aspose.ZIP for .NET API. Ensure you have the API installed using one of the following methods:
- Install via NuGet Package Manager.
- Download the DLL and add its reference to your project.
Create 7z (7-Zip) Archive using C#
You can compress files into a 7z archive in two primary scenarios: when dealing with a single file or multiple files. Here’s how to handle both situations.
Create 7z Archive with Single Entry
To create a 7z archive with a single file, follow these steps:
- Open a new FileStream to create a 7z file.
- Create an instance of the SevenZipArchive class.
- Add the file to the archive using the SevenZipArchive.CreateEntry(String, String, Boolean, SevenZipEntrySettings) method.
- Save the archive using the SevenZipArchive.Save(Stream) method.
Here’s a C# 7z archive example demonstrating the above steps:
Create 7z Archive with Multiple Entries
If you need to compress multiple files, you can place them in a folder and pass the folder’s path to the SevenZipArchive.CreateEntries() method. Follow these steps:
- Create an instance of the SevenZipArchive class.
- Pass the folder’s path to the SevenZipArchive.CreateEntries(String, Boolean) method.
- Call the SevenZipArchive.Save(String) method.
Here’s how to create a 7z archive with multiple entries using C#:
Create an AES Encrypted 7z Archive using C#
The 7z format supports AES encryption for added security. To encrypt a 7z file, use the SevenZipAESEncryptionSettings class. Below is a code sample that demonstrates how to create an AES encrypted 7z archive using C#:
Set Different Passwords for 7z Entries using C#
The 7z format also allows you to assign a unique password for each file entry. If you’re compressing multiple files, you can specify different passwords for each. Here’s how to do it:
- Create a new FileStream for the 7z archive file.
- Access each file entry using the FileInfo class.
- Create an instance of the SevenZipArchive class.
- Add entries to the 7z archive using the SevenZipArchive.CreateEntry(String, FileInfo, Boolean, SevenZipEntrySettings) method.
- Save the archive using the SevenZipArchive.Save(Stream) method.
Check out the following code sample to see how to set different passwords for each entry in a 7z archive:
Conclusion
In this article, we’ve covered how to create 7z archives programmatically using C#. We also explored how to implement AES encryption for enhanced security and how to set unique passwords for each file entry. For further reading on file compression and decompression, you can check out the Aspose.ZIP documentation.
Related Articles
By leveraging the Aspose Plugin for .NET, you can easily integrate .NET 7-Zip compression into your applications, streamline your file management processes, and enhance your software’s capabilities. For just $99, you can unlock powerful features that support large file handling, asynchronous compression, and more.