Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
ZIP files are files that contain one or more compressed files or folders, using the ZIP format. It is a common way of compressing and archiving multiple files or folders into a single file. They can reduce the size of the data, save disk space, and make it easier to transfer files over the internet. In this article, you will learn how to work with ZIP files in C#, using the IronZIP library. You will see how to create, read, extract, and update ZIP files programmatically, as well as how to use various features of IronZIP, such as encryption, password protection, and compression levels. By the end of this article, you will be able to use IronZIP to handle ZIP files in your C# applications with ease.
IronZIP is a powerful and versatile C# ZIP archive library that allows you to create, read, and extract ZIP files programmatically. It supports various archive formats, such as ZIP, TAR, GZIP, and BZIP2. It also supports password protection, encryption, and compression levels. IronZIP is compatible with .NET 8, 7, 6, Core, Standard, and Framework.
IronZIP can help you handle various use cases and benefits of working with ZIP files, such as:
Before writing the code, you need to install the IronZIP NuGet Package our C# project. IronZIP is a popular compression library available via NuGet.
To install IronZIP, you can use the NuGet Package Manager Console in Visual Studio. Simply run the following command:
Install-Package IronZip
Alternatively, you can download the package directly from the official IronZIP website. Once installed, you can get started by adding the following namespace to the top of your C# code.
using IronZip;
using IronZip;
Imports IronZip
We can easily create ZIP files in a folder using IronZIP. The following code will zip all the files from the specified directory.
static void Main(string [] args)
{
string [] fileArray = Directory.GetFiles(@"D:\Docs\");
using (var archive = new IronZipArchive())
{
foreach (var file in fileArray)
{
// Add files to the ZIP
archive.Add(file);
}
// Export the ZIP
archive.SaveAs("myZipFile.zip");
}
}
static void Main(string [] args)
{
string [] fileArray = Directory.GetFiles(@"D:\Docs\");
using (var archive = new IronZipArchive())
{
foreach (var file in fileArray)
{
// Add files to the ZIP
archive.Add(file);
}
// Export the ZIP
archive.SaveAs("myZipFile.zip");
}
}
Shared Sub Main(ByVal args() As String)
Dim fileArray() As String = Directory.GetFiles("D:\Docs\")
Using archive = New IronZipArchive()
For Each file In fileArray
' Add files to the ZIP
archive.Add(file)
Next file
' Export the ZIP
archive.SaveAs("myZipFile.zip")
End Using
End Sub
The above C# code uses the IronZIP library to compress all the files into a single ZIP file. The code does the following:
In this way, we can easily create new zip archives using just a few lines of code.
Output is as:
IronZIP provides the easiest method to create password-protected ZIP files. The following code sample will compress files and create a new ZIP file with a password.
using (var archive = new IronZipArchive())
{
foreach (var file in fileArray)
{
// Add files to the ZIP
archive.Add(file);
}
// Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256);
// Export the ZIP
archive.SaveAs("myZipFile.zip");
}
using (var archive = new IronZipArchive())
{
foreach (var file in fileArray)
{
// Add files to the ZIP
archive.Add(file);
}
// Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256);
// Export the ZIP
archive.SaveAs("myZipFile.zip");
}
Using archive = New IronZipArchive()
For Each file In fileArray
' Add files to the ZIP
archive.Add(file)
Next file
' Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256)
' Export the ZIP
archive.SaveAs("myZipFile.zip")
End Using
The line archive.Encrypt("myPa55word", EncryptionMethods.AES256); sets a password ("myPa55word") for a ZIP archive using IronZIP. It enhances security by applying AES-256 encryption to the archive, ensuring that only users with the correct password can access its contents. This feature is valuable for protecting sensitive data during storage or transfer within C# applications. We need to pass the specified mode of Encryption algorithm in the second parameter.
The file is encrypted as shown below.
We have seen the demonstration of creating a ZIP file by looping through the directories from the specified path. Now, let's move towards the example of unzipping files.
IronZIP provides a method to extract files from a ZIP archive C#. The following code sample will extract the compressed file within a ZIP archive.
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles");
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles");
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles")
The code IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles"); uses IronZIP to extract all files from "myZipFile.zip" and places them in the "myExtractedFiles" directory. This concise method simplifies the process of extracting ZIP archives in C#, providing a convenient solution for file extraction tasks.
Output is as so:
IronZIP also provides a method to extract password-protected ZIP files. The following code will use the IronZIP method to extract all the existing files and directories from the specified ZIP file.
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word");
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word");
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word")
The ExtractArchiveToDirectory method of the IronZipArchive class extracts all the entries from a ZIP archive to a specified directory. It passes three arguments to the method: the path of the ZIP file (“myZipFile.zip”), the path of the destination directory (“myExtractedFiles”), and the password of the ZIP file (“myPa55word”).
In this way, we can easily extract password-protected zip files.
IronZIP provides methods to access the existing archive and view all the entries present in the file.
using (var archive = new IronZipArchive("myZipFile.zip", "myPa55word"))
{
// Get Entries list
List<string> names = archive.GetArchiveEntryNames();
foreach (string name in names)
{
Console.WriteLine(name);
}
}
using (var archive = new IronZipArchive("myZipFile.zip", "myPa55word"))
{
// Get Entries list
List<string> names = archive.GetArchiveEntryNames();
foreach (string name in names)
{
Console.WriteLine(name);
}
}
Using archive = New IronZipArchive("myZipFile.zip", "myPa55word")
' Get Entries list
Dim names As List(Of String) = archive.GetArchiveEntryNames()
For Each name As String In names
Console.WriteLine(name)
Next name
End Using
The provided C# code uses IronZIP to create a secure IronZipArchive instance by loading a ZIP file named "myZipFile.zip" with the password "myPa55word". Don't pass the password parameter if the file is not encrypted. It then retrieves and prints the list of entry names (file and folder names) within the encrypted ZIP archive.
The GetArchiveEntryNames method gathers the entry names, and a for-each loop outputs each name to the console. This demonstrates how IronZIP enables secure access and retrieval of entry information from password-protected zip archives in a concise manner.
In conclusion, IronZIP proves to be a robust and versatile C# library for working with ZIP files. Its capabilities extend beyond basic compression and extraction, offering features such as password protection, encryption, and compatibility with various archive formats. Whether you're creating backup systems, managing email attachments, or downloading files from the web, IronZIP streamlines these tasks with simplicity and efficiency.
By integrating IronZIP into your C# applications, you gain a powerful tool for handling ZIP files, enhancing data security, and optimizing file transfer processes. You can benefit from a free trial as per need.
9 .NET API products for your office documents