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

What is a Zip Archive?

A Zip Archive is a popular file format for compressing and packing a single file or multiple files into one archive, making it easier to send or store large amounts of data.

How can I create a new zip file using IronZip?

You can create a new zip file in C# using IronZip by first importing the IronZip library, creating an IronZipArchive instance, adding files with the archive.Add() method, and then exporting it using archive.SaveAs().

How do I update an existing zip file in C#?

To update an existing zip file using IronZip, you open the zip with IronZipArchive.FromFile(), add new files using the archive.Add() method, and save the updated archive with archive.SaveAs().

Can IronZip extract files from a zip archive?

Yes, IronZip can extract files using the IronZipArchive.ExtractArchiveToDirectory() method, which extracts the contents to a specified directory.

How do I handle password-protected zip archives with IronZip?

For password-protected zip archives, you can use IronZipArchive.ExtractArchiveToDirectory() with an additional parameter for the password to extract the contents securely.

What advanced features does IronZip offer?

IronZip supports cross-compatibility with .NET versions, creating and extracting various archive formats, password protection, custom compression levels, and editing archives by adding or deleting file entries.

What file types can IronZip handle?

IronZip can handle images, text files, documents like PDFs, DOCX, XLSX, and audio files like MP3 and WAV. It also supports compressing entire file systems.

How can developers adjust compression settings in IronZip?

Developers can adjust compression settings in IronZip by using the custom compression levels feature to fine-tune the algorithm according to their needs.

Does IronZip support different licensing options?

Yes, IronZip offers adaptable licensing choices, including complimentary trial versions, allowing developers to choose the option that best suits their requirements.

Why should developers use IronZip over System.IO.Compression?

Developers might choose IronZip over System.IO.Compression for its advanced features, such as creating and extracting various formats, password protection, and comprehensive editing capabilities, which increase efficiency and automation in handling archives.

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 sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
A Comparison between IronXL and GemBox.Spreadsheet
NEXT >
EPPlus Read Excel to Datatable C# (IronXL Tutorial)