Create, Read, and Extract Zip Tutorial

Create ZIP generates a new ZIP archive by selecting files or directories, specifying compression settings, and creating the archive.

Read ZIP opens an existing ZIP archive to access its contents for viewing or extracting specific files.

Extract ZIP retrieves contents by specifying the source ZIP file, the destination folder, and extracting files and directories to the specified location.

In addition to these functions, IronZip can also open an existing ZIP file, add more files to it, and then export the result as a new ZIP file containing all the included files.

C# NuGet Library for

Install with NuGet

Install-Package IronZip

Create an Archive Example

To create a ZIP file, you can conveniently use the 'using' statement in C# and specify the desired output ZIP file name. IronZip makes this process straightforward, allowing you to establish an empty ZIP archive with just a few lines of code.

Next, you can utilize the AddArchiveEntry method to import your files into the ZIP archive. This method allows you to add files from various locations, including an entire directory where all the files within it will be included.

:path=/static-assets/zip/content-code-examples/tutorials/create-read-extract-zip-create.cs
using IronZip;

// Create an empty zip
using (var archive = new IronArchive("output.zip"))
{
    // Add files to the zip
    archive.AddArchiveEntry("./assets/image1.png");
    archive.AddArchiveEntry("./assets/image2.png");
}
Imports IronZip

' Create an empty zip
Using archive = New IronArchive("output.zip")
	' Add files to the zip
	archive.AddArchiveEntry("./assets/image1.png")
	archive.AddArchiveEntry("./assets/image2.png")
End Using
VB   C#

Unarchive an Archive to Folder

To retrieve the contents from a ZIP file, you can use the ExtractArchiveToDirectory method. Simply indicate the path of the target ZIP file and the directory where you want the extracted files to be placed.

:path=/static-assets/zip/content-code-examples/tutorials/create-read-extract-zip-extract.cs
using IronZip;

// Extract Zip
IronArchive.ExtractArchiveToDirectory("output.zip", "extracted");
Imports IronZip

' Extract Zip
IronArchive.ExtractArchiveToDirectory("output.zip", "extracted")
VB   C#

Add Files to An Existing Archive

You can efficiently modify an existing ZIP archive with additional files using IronZip. The process begins by using the FromFile method to open the archive you want to modify. This method not only opens the existing ZIP but also requires specifying the name of the new archive that will be created as the output. Once the archive is opened, you can use the AddArchiveEntry method, the same one used for creating new ZIP files, to add files to the existing archive.

:path=/static-assets/zip/content-code-examples/tutorials/create-read-extract-zip-add-files.cs
using IronZip;

// Open existing zip and export zip
using (var archive = IronArchive.FromFile("existing.zip", "result.zip"))
{
    // Add files
    archive.AddArchiveEntry("./assets/image3.png");
    archive.AddArchiveEntry("./assets/image4.png");
}
Imports IronZip

' Open existing zip and export zip
Using archive = IronArchive.FromFile("existing.zip", "result.zip")
	' Add files
	archive.AddArchiveEntry("./assets/image3.png")
	archive.AddArchiveEntry("./assets/image4.png")
End Using
VB   C#

With this functionality, you can efficiently update and expand your ZIP archives to suit your project's evolving needs. IronZip makes the process of managing archives in your C# projects a breeze.

A similar approach can be achieved for other archive and compression formats such as tar, gzip, and bzip2 using the IronArchive, IronArchive, and IronArchive classes, respectively.