Skip to footer content
COMPARE TO OTHER COMPONENTS

Zip Archive Tutorial for C# Developers Using IronXL

Introduction to ZipArchive

Whenever you think of sending a file over in a compressed format, the first thing that comes to mind is usually a Zip Archive. A zip archive is a popular format for compressing and packing a single file or an entire collection in a single archive. However, when there are large volumes of files to zip, working with them might become frustrating as you would have to archive and format them one by one. But it doesn't have to be like that. Large-volume tasks need automation to be done programmatically. We can use IronZip, a simple yet intuitive library, to achieve all that and more.

In this article, we'll briefly discuss IronZip's core features and functionalities and how it can help you better automate your workflow, increase efficiency, and eliminate error-prone manual tasks.

Creating a new zip file from a folder or files

Below is an example that takes in multiple files, zips them into one archive, and exports them after the operation. All examples in this article are encased in the static void Main() method to avoid repeated code blocks.

using IronZip;

class Program
{
    static void Main(string[] args)
    {
        // Create an empty ZIP archive
        using (var archive = new IronZipArchive())
        {
            // Add files to the ZIP archive
            archive.Add("./assets/image1.jpg");
            archive.Add("./assets/image2.jpg");
            archive.Add("./assets/image3.jpg");

            // Export the ZIP archive to a file
            archive.SaveAs("output.zip");
        }
    }
}
using IronZip;

class Program
{
    static void Main(string[] args)
    {
        // Create an empty ZIP archive
        using (var archive = new IronZipArchive())
        {
            // Add files to the ZIP archive
            archive.Add("./assets/image1.jpg");
            archive.Add("./assets/image2.jpg");
            archive.Add("./assets/image3.jpg");

            // Export the ZIP archive to a file
            archive.SaveAs("output.zip");
        }
    }
}
Imports IronZip

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create an empty ZIP archive
		Using archive = New IronZipArchive()
			' Add files to the ZIP archive
			archive.Add("./assets/image1.jpg")
			archive.Add("./assets/image2.jpg")
			archive.Add("./assets/image3.jpg")

			' Export the ZIP archive to a file
			archive.SaveAs("output.zip")
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel
  1. We first import IronZip.
  2. Then, using IronZip, we create an archive.
  3. We call archive.Add() to add multiple files to the archive. Remember that path names must be absolute paths, or the operation will fail to find the corresponding files.
  4. Finally, we call archive.SaveAs() to export the zip archive as output.zip.

Updating an existing zip file with new files or modifications

Let's go over another example; this time, we will edit the existing zip archive with new files to showcase the functionality of IronZip.

using IronZip;

class Program
{
    static void Main(string[] args)
    {
        // Open an existing ZIP archive
        using (var archive = IronZipArchive.FromFile("existing.zip"))
        {
            // Add additional files to the existing ZIP archive
            archive.Add("./assets/image3.png");
            archive.Add("./assets/image4.png");

            // Export the updated ZIP archive to a new file
            archive.SaveAs("result.zip");
        }
    }
}
using IronZip;

class Program
{
    static void Main(string[] args)
    {
        // Open an existing ZIP archive
        using (var archive = IronZipArchive.FromFile("existing.zip"))
        {
            // Add additional files to the existing ZIP archive
            archive.Add("./assets/image3.png");
            archive.Add("./assets/image4.png");

            // Export the updated ZIP archive to a new file
            archive.SaveAs("result.zip");
        }
    }
}
Imports IronZip

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Open an existing ZIP archive
		Using archive = IronZipArchive.FromFile("existing.zip")
			' Add additional files to the existing ZIP archive
			archive.Add("./assets/image3.png")
			archive.Add("./assets/image4.png")

			' Export the updated ZIP archive to a new file
			archive.SaveAs("result.zip")
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel
  1. We first import IronZip.
  2. Using IronZip, we create an archive, but this time, we import the existing zip file with IronZipArchive.FromFile().
  3. We call archive.Add() to add the desired files to the archive. Remember that path names must be absolute paths, or the operation will fail to find the existing files.
  4. Finally, we call archive.SaveAs() to export the updated zip archive as result.zip.

As you can see from the code above, the operation and format are similar to those used to create and add files to a Zip file. The main difference is that we import the zip file instead, showcasing IronZip's simple yet intuitive functionality.

Extracting files from a Zip archive

We can also use IronZip to extract files from a Zip archive or Zip package. Let's review this in another example.

using IronZip;

class Program
{
    static void Main(string[] args)
    {
        // Extract the ZIP archive content to a specified directory
        IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted");
    }
}
using IronZip;

class Program
{
    static void Main(string[] args)
    {
        // Extract the ZIP archive content to a specified directory
        IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted");
    }
}
Imports IronZip

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Extract the ZIP archive content to a specified directory
		IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted")
	End Sub
End Class
$vbLabelText   $csharpLabel

We import IronZip and call IronZipArchive.ExtractArchiveToDirectory(). This method extracts the contents of the existing zip archive to the specified target directory.

Furthermore, in cases where you need to deal with password-protected zip archives, we can use another method to extract the archives.

using IronZip;

class Program
{
    static void Main(string[] args)
    {
        // Extract the protected ZIP archive content to a specified directory
        IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd");
    }
}
using IronZip;

class Program
{
    static void Main(string[] args)
    {
        // Extract the protected ZIP archive content to a specified directory
        IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd");
    }
}
Imports IronZip

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Extract the protected ZIP archive content to a specified directory
		IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd")
	End Sub
End Class
$vbLabelText   $csharpLabel

Ultimately, we pass in another parameter: the password for the protected zip archive. The rest of the operation is the same as shown above.

Advanced Topics and Best Practices

The code examples for adding, extracting, and creating archives discussed above are the most common when dealing with zip archives. However, for more advanced usage, such as extracting other formats or simply viewing the archive's contents, IronZIP has you covered and more.

Advanced Features of IronZIP

  • Cross-compatibility: IronZIP is compatible with a wide range of .NET versions, including .NET Core (3.1+), Standard (2.0+), and .NET Framework (4.6.2+). The library also works on the Web (Blazor), Mobile (MAUI), Desktop (WPF), and Console. This allows developers to transcend platform and version limitations.

  • Generating Archives: Beyond the ZIP format, IronZIP supports creating TAR, GZIP, and BZIP2 archives.

  • Extracting Archives: Extract archives and decompress files easily with IronZIP using a single block of code, as demonstrated above.

  • Adding Files and File Systems: IronZIP assists in manipulating zip archives by adding images, text files, and documents (such as PDFs, DOCX, and XLSX), as well as audio files like MP3 and WAV. It can even compress entire file systems or individual text files into a ZipArchive archive.

  • Export and Create: You can password-protect archives with AES128 and AES256 standards. Additionally, you can generate and export formats like TAR, GZIP, and BZIP2.

  • Custom Compression Levels: IronZIP allows developers to adjust compression settings to fine-tune the algorithm to their needs.

  • Editing Archives: Easily add, extract, or delete file entries in an archive using IronZIP as a comprehensive solution for editing-related operations.

  • File Entry Properties: IronZIP provides the ability to set optional archive comments and retrieve file names within the archive without extracting them, aiding in leaving specific comments for each file.

  • Licensing Choices: IronZIP offers adaptable licensing choices, including complimentary trial versions, allowing developers to select what suits their requirements best.

Conclusion

Dealing with compressed files and archives is a daily task that developers often face, but managing a large volume of them can be stressful and prone to human error. Although there are options like System.IO.Compression, using IronZIP allows you to achieve more and quickly resolve issues when dealing with compressed archives. In this article, we discussed the advanced features of IronZIP and common operations like creating zip archives, deleting entries, or adding files. Understanding how to perform these tasks programmatically with IronZIP leads to increased efficiency, scalability, and automation in handling archives.

Frequently Asked Questions

How do I create a ZIP file from a folder in C#?

To create a ZIP file from a folder in C#, you can use the IronZip library. First, import the IronZip library, create an instance of IronZipArchive, add the folder using archive.AddFolder(), and save it with archive.SaveAs().

What is the process to extract data from a ZIP archive in C#?

You can extract data from a ZIP archive in C# using IronZip by calling the IronZipArchive.ExtractArchiveToDirectory() method. This method allows you to specify a directory where the contents will be extracted.

How can I update contents of a ZIP archive programmatically in C#?

To update contents of a ZIP archive programmatically in C#, use IronZip by loading the existing archive with IronZipArchive.FromFile(), then use archive.Add() to add files or archive.Remove() to delete files before saving the changes with archive.SaveAs().

Can IronZip handle password-protected ZIP files?

Yes, IronZip can handle password-protected ZIP files. You can specify a password when extracting files using the IronZipArchive.ExtractArchiveToDirectory() method by providing the password as an additional parameter.

What are the benefits of using IronZip over System.IO.Compression for ZIP file handling?

IronZip offers advanced features over System.IO.Compression such as support for multiple archive formats, password protection, custom compression levels, and the ability to edit archives by adding or deleting files without extracting them.

How does IronZip ensure cross-compatibility with different .NET versions?

IronZip ensures cross-compatibility by supporting multiple .NET versions, allowing developers to integrate ZIP file handling functionality into applications running on various platforms and frameworks.

What advanced features does IronZip offer for handling ZIP archives?

IronZip offers advanced features like generating and extracting multiple archive formats (ZIP, TAR, GZIP, BZIP2), password protection, custom compression levels, and comprehensive editing capabilities without needing to extract files.

Is it possible to manage file properties within a ZIP archive using IronZip?

Yes, IronZip allows you to manage file properties such as comments within a ZIP archive without needing to extract the files first, enhancing flexibility and efficiency in archive management.

How can I automate ZIP file creation and extraction in C#?

You can automate ZIP file creation and extraction in C# by using IronZip in a static void Main() method. This allows you to automate processes like adding files to an archive, extracting data, and updating existing archives programmatically.

Does IronZip support the handling of entire file systems?

Yes, IronZip supports the compression and extraction of entire file systems, making it suitable for handling large volumes of data efficiently.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to ...Read More