C# Tutorial: Create, Read & Extract ZIP Files
Create ZIP generates a new ZIP archive by selecting files or directories, specifying compression settings, and creating the archive.
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.
Quickstart: Create or Extract a ZIP with IronZIP Easily
Get started fast—create, modify, or extract ZIP archives using IronZIP in just a few simple API calls. Perfect for developers who want to instantly work with archives without boilerplate.
Get started making PDFs with NuGet now:
Install IronZIP with NuGet Package Manager
Copy and run this code snippet.
IronZipArchive.ExtractArchiveToDirectory("project.zip", "outputFolder");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download the C# library to create, read, and extract ZIP files
- Instantiate the IronZipArchive class to create an empty ZIP file
- Use the
Addmethod to add files to the empty ZIP - Utilize the
ExtractArchiveToDirectorymethod to extract the ZIP - Open an existing ZIP file by passing the ZIP file path to the constructor
Create an Archive Example
To create a ZIP archive object, you can conveniently use the using statement in C# along with the IronZipArchive constructor. IronZip makes this process straightforward, allowing you to establish an empty ZIP archive with just a few lines of code.
Next, utilize the Add 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.
Lastly, use the SaveAs method to export the ZIP file.
:path=/static-assets/zip/content-code-examples/tutorials/create-read-extract-zip-create.csusing IronZip;
// Create an empty ZIP
using (var archive = new IronZipArchive())
{
// Add files to the ZIP
archive.Add("./assets/image1.png");
archive.Add("./assets/image2.png");
// Export the ZIP file
archive.SaveAs("output.zip");
}Imports IronZip
' Create an empty ZIP
Using archive = New IronZipArchive()
' Add files to the ZIP
archive.Add("./assets/image1.png")
archive.Add("./assets/image2.png")
' Export the ZIP file
archive.SaveAs("output.zip")
End UsingUnarchive 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.csusing IronZip;
// Extract ZIP
IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted");Imports IronZip
' Extract ZIP
IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted")Add Files to An Existing Archive
You can efficiently modify an existing ZIP archive with additional files using IronZip. The process begins by instantiating the ZIP archive object from an existing ZIP file path. Once the archive is opened, you can use the Add method to add files to the existing archive.
:path=/static-assets/zip/content-code-examples/tutorials/create-read-extract-zip-add-files.csusing IronZip;
// Open existing ZIP
using (var archive = IronZipArchive.FromFile("existing.zip"))
{
// Add files
archive.Add("./assets/image3.png");
archive.Add("./assets/image4.png");
// Export the ZIP file
archive.SaveAs("result.zip");
}Imports IronZip
' Open existing ZIP
Using archive = IronZipArchive.FromFile("existing.zip")
' Add files
archive.Add("./assets/image3.png")
archive.Add("./assets/image4.png")
' Export the ZIP file
archive.SaveAs("result.zip")
End UsingWith 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 IronTarArchive, IronGZipArchive, and IronBZip2Archive classes, respectively.
Frequently Asked Questions
How can I create a ZIP archive in C#?
To create a ZIP archive in C#, instantiate the IronZipArchive class from IronZip. Use the Add method to add files or directories to the archive and the SaveAs method to save the archive. Example code: using (var zip = new IronZipArchive()) { zip.Add(@"C:\path\to\directory"); zip.SaveAs(@"C:\path\to\output.zip"); }.
How do I read and extract contents from a ZIP file in C#?
You can read and extract contents from a ZIP file by using the IronZipArchive class to open the ZIP file. Utilize the ExtractArchiveToDirectory method to specify the destination directory for the extracted files. Example: using (var zip = new IronZipArchive(@"C:\path\to\archive.zip")) { zip.ExtractArchiveToDirectory(@"C:\path\to\output\directory"); }.
Can I add files to an existing ZIP archive in C#?
Yes, you can add files to an existing ZIP archive in C#. Open the existing ZIP using the IronZipArchive class, use the Add method to include additional files, and then save the updated archive with the SaveAs method.
What libraries can be used for ZIP management in C#?
IronZip is a comprehensive library available on NuGet for managing ZIP files in C#. It allows you to create, read, and extract ZIP files efficiently using classes like IronZipArchive.
How do I handle other compression formats like TAR and GZIP in C#?
In addition to ZIP files, you can manage other compression formats using IronZip. For TAR, GZIP, and BZIP2, use the IronTarArchive, IronGZipArchive, and IronBZip2Archive classes, respectively, to handle these formats similarly.
How can I begin working with ZIP files in C#?
Start by downloading the IronZip library from NuGet. Instantiate the IronZipArchive class to create or open ZIP files, and use methods like Add and ExtractArchiveToDirectory to manage the archives.
Is it possible to extract specific files from a ZIP archive in C#?
Yes, it is possible to extract specific files from a ZIP archive in C#. Open the ZIP file using IronZipArchive and access the desired files for extraction using the library's provided methods.





