How to Compress or Extract Archives using C#
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#
- Create an instance of Archive class.
- Add files using Archive.CreateEntry method.
- 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#
- Create an instance of Archive class with your zip file.
- 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#
- Create an instance of SevenZipArchive class with SevenZipLZMACompressionSettings.
- Add your files by calling SevenZipArchive.CreateEntries method.
- 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#
- Create an instance of SevenZipArchive class with your 7z file.
- 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#
- Create an instance of RarArchive class with your RAR file.
- 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#
- Create an instance of Archive class.
- Pass it ArchiveEntrySettings and set encryptionSettings property with the object of TraditionalEncryptionSettings having your password.
- Add as many files by calling Archive.CreateEntry method multiple times.
- 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#
- Create an instance of Archive class.
- Pass it ArchiveLoadOptions object and set DecryptionPassword property with your zip file password.
- 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
- Compression
- C# Zip File
- C# Zip Files
- C# Create Zip Archive
- C# Zip Multiple Files
- C# Add File to Zip
- C# Compress files to Zip
- C# Archives files to Zip
- C# How to Zip a File
- Extraction
- C# UnZip File
- C# UnZip Files
- C# Extract Zip Archive
- C# How to UnZip a File
- Compression Password Protected
- C# Zip File with Password
- C# Zip Files with Password
- C# Compress files to Password Protected Zip
- C# Archives files to Zip with Password Protection
- Extraction Password Protected
- C# UnZip Password Protected File
- C# UnZip Password Protected Files
- C# Extract Password Protected Zip Archive
- C# How to UnZip a File with Password Protection
Format: 7z
- Compression
- C# 7z File
- C# 7z Files
- C# Create 7z Archive
- C# 7z Multiple Files
- C# Add File to 7z
- C# Compress files to 7z
- C# Archives files to 7z
- C# How to 7z a File
- Extraction
- C# Decompress 7z File
- C# Decompress 7z Files
- C# Extract 7z File
- C# Extract 7z Files
- C# Extract 7z Archive
- C# How to Extract 7z File
Format: RAR