Create 7Zip Archive in C#

In the previous post, I have discussed different ways of creating ZIP archives using C#. The article also covered how to create encrypted and password-protected ZIP archives. Besides ZIP format, there are some other commonly used archive formats such as 7z, tar, RAR, etc. Among these, 7z archive format is based on open-source architecture and it is used to compress files and folders with a high compression ratio. It also supports 256-bit AES encryption and allows encrypting the files’ names as well. In this article, I’ll demonstrate how to create 7z (7-Zip) archives programmatically using C#.

This article is divided into the following sections:

All the steps and code samples in this article are based on Aspose.ZIP for .NET. Therefore, make sure you have installed the API using either of the following methods:

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

There could be two possible scenarios of compressing files into a 7z archive. Either you have only a single file or there could be a bunch of files to be compressed. You can deal with both of the scenarios explicitly.

Create 7z Archive with Single Entry

The following are the steps to create a 7z archive with a single entry.

The following code sample shows how to create a 7z (7-Zip) archive using C#.

Create 7z Archive with Multiple Entries

In this case, you can put the files in a folder and pass the folder’s path to SevenZipArchive.CreateEntries() method. The following are steps to add multiple file entries to a 7z archive.

The following code sample shows how to create a 7z archive with multiple entries using C#.

Create an AES Encrypted 7z Archive using C#

7z format supports AES encryption to secure the files. In order to encrypt a 7z file, the SevenZipAESEncryptionSettings class is used. The following code sample shows how to create an AES encrypted 7z archive using C#.

Set Different Passwords for 7z Entries using C#

7z format also allows you to specify a different password for every file entry. For instance, if you are going to compress two files in a 7z archive, you can specify a different password for each file. The following are the steps to set different passwords for 7z entries.

The following code sample shows how to set a password for every entry in a 7z archive.

Conclusion

In this article, we have learned how to create 7z archives programmatically using C#. In addition, we have seen how to create AES encrypted 7z files and set a password for every file entry. You can learn more about compression/decompression of files from here.