How to Compress or Extract Archives using C# | Aspose.Zip for .NET

Overview

This article explains how to compress or extract archives. These archives could be Zip, 7z, Rar and other such formats. It covers the following topics.

Format: ZIP

Format: ZIP - Password

Format: 7z

Format: RAR

Other topics covered by this article.

C# Compress or Extract Archives

This page shows how to perform standard archive operations in C# using Aspose.ZIP for .NET. Aspose.ZIP package can be added to you .NET project using the following NuGet command:

1PM> Install-Package Aspose.Zip

It can also be added directly to .NET project as a assembly reference. Aspose.ZIP library can be downloaded from our home page: https://products.aspose.com/zip/net/.

Aspose.ZIP .NET library can be used on any OS that supports .NET Core (e.g. Windows, Linux or macOS).

How to create ZIP files in C#

Steps: Create Zip file in C#

  1. Create an instance of Archive class.
  2. Add files using Archive.CreateEntry method.
  3. Zip the files using Archive.Save method.
1using (var archive = new Archive())
2{
3   archive.CreateEntry("entry_name1.dat", "input_file1.dat");
4   archive.CreateEntry("entry_name2.dat", "input_file2.dat");
5   archive.Save("result_archive.zip");
6}

Archive class constructor creates a regular ZIP archive using Deflate compression algorithm and no encryption.

How to UnZIP files in C#

Steps: Unzip File to Folder in C#

  1. Create an instance of Archive class with your zip file.
  2. UnZip the zip file using Archive.ExtractToDirectory method to your folder.
1using (var archive = new Archive("input_archive.zip"))
2{
3   archive.ExtractToDirectory("\\outputDirectory");
4}

Archive class constructor can open any ZIP archive. ArchiveLoadOptions should be provided as a second parameter in case the input archive is password-protected (See example below).

How to compress 7z files in C#

Steps: Create 7z archives in C#

  1. Create an instance of SevenZipArchive class with SevenZipLZMACompressionSettings.
  2. Add your files by calling SevenZipArchive.CreateEntries method.
  3. Save it to 7z format by calling SevenZipArchive.Save method.
1using (var archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings())))
2{
3	archive.CreateEntries(CommonConstants.TestFilesFolder + "ManyFilesToCompress");
4	archive.Save(CommonConstants.TestOutFolder + "7Z_ManyFilesToCompress.7z");
5}

SevenZipArchive class constructor should be used to create a new 7z archive. The above sample uses classic LZMA compression, but other compression as well as encryption options are also available.

How to extract 7z files in C#

Steps: Extract 7z file in C#

  1. Create an instance of SevenZipArchive class with your 7z file.
  2. Extract the contents of 7z file by calling SevenZipArchive.ExtractToDirectory method.
1using (var archive = new SevenZipArchive("input_archive.7z"))
2{
3	archive.ExtractToDirectory("\\outputDirectory");
4}

In the above sample SevenZipArchive class constructor opens non-protected 7z archive.

How to extract RAR files in C#

Steps: Extract RAR file in C#

  1. Create an instance of RarArchive class with your RAR file.
  2. Extract the contents of RAR file by calling RarArchive.ExtractToDirectory method.
1using (var archive = new RarArchive("input_archive.rar"))
2{
3	archive.ExtractToDirectory("\\outputDirectory");
4}

In the above sample RarArchive class constructor opens non-protected RAR archive.

How to ZIP files with password in C#

Steps: Create Zip file with Password in C#

  1. Create an instance of Archive class.
  2. Pass it ArchiveEntrySettings and set encryptionSettings property with the object of TraditionalEncryptionSettings having your password.
  3. Add as many files by calling Archive.CreateEntry method multiple times.
  4. Save it as Zip file using Archive.Save method.
1using (var archive = new Archive(new ArchiveEntrySettings(encryptionSettings: new TraditionalEncryptionSettings("pass"))))
2{
3   archive.CreateEntry("entry_name1.dat", "input_file1.dat");
4   archive.CreateEntry("entry_name2.dat", "input_file2.dat");
5   archive.Save("result_archive.zip");
6}

The encryptionSettings parameter is used to create a password-protected ZIP archive.

How to UnZIP files with password in C#

Steps: Unzip Password Protected Zip file using C#

  1. Create an instance of Archive class.
  2. Pass it ArchiveLoadOptions object and set DecryptionPassword property with your zip file password.
  3. UnZip the zip file using Archive.ExtractToDirectory method to your folder.
1using (var archive = new Archive("input_archive.zip", new ArchiveLoadOptions{DecryptionPassword = "pass"}))
2{
3   archive.ExtractToDirectory("\\outputDirectory");
4}

The ArchiveLoadOptions with DecryptionPassword property value is used to open a password-protected ZIP archive.

See Also

This article also covers these topics. The codes are same as above.

Format: ZIP

Format: 7z

Format: RAR

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.