Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
ZIP is an archive entry file system format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The ZIP file format permits several compression algorithms, though DEFLATE is the most common. ZIP format was then quickly supported by many software utilities. Top OS providers have included ZIP archive support for a long time. Microsoft has included ZIP file support from Windows 98 and the rest followed suit.
In this blog, we are going to explore a modern, easy, and efficient way to open ZIP archive files or extracted files using IronZIP. We are going to learn about ZIP files in general and its advantages. Then we will see options available in the system namespace for working with ZIP file format. Then we will explore step-by-step instructions to open the ZIP file, extract ZIP file to a temp folder, create a new ZIP file, and add files to existing ZIP files.
Introduction to the IronZIP library and documentation can be found here. In C# applications ZIP files can be created and extracted in various ways. IronZIP NuGet package has all the functionalities to archive files in different formats ZIP, TAR, GZIP, and BZIP2. Below are the sample steps on how to use IronZIP in modern application programming to build next-gen apps to open ZIP files, extract ZIP files, create new ZIP files, etc.
.NET console app can be created using Visual Studio. Open Visual Studio and select Create project. Here you can see various templates to create a project. The easiest way to demonstrate or test the code is by creating a console application. We will select the Console App project template.
In the below window, you can enter the project name, the location of the project to store in the file system and last one is the path to the solution folder. You can keep the solution and project folders the same or have them in different folders.
The next step is to select the .NET Framework version for the project. If you want to develop in specific version then specify the version you like, else always select the latest stable version to create a project. The latest version can be downloaded from the Microsoft website. Then click Create to generate the console application.
This will create the default project from the template and store the project and solution files in the specified directories. Once the project is created it will look similar to below. Sometimes the class is not used in the program.cs in the latest versions.
using System;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program
{
static void Main(string [] args)
{
Console.WriteLine("Hello World!");
}
}
}
using System;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program
{
static void Main(string [] args)
{
Console.WriteLine("Hello World!");
}
}
}
Imports System
' See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!")
Dim MyApp As namespace ' Note: actual namespace depends on the project name.
If True Then
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' internal class Program
' {
' static void Main(string [] args)
' {
' Console.WriteLine("Hello World!");
' }
' }
End If
To create a new ZIP file and extract all the ZIP archive files, we can use System.IO.Compression from default libraries. In the above code ZipFile.OpenRead
and ZipFile.Open
static methods can be used to open ZIP files or extract ZIP files.
using System.IO.Compression;
Console.WriteLine("-----------Zip - Unzip-----------");
static void AddEntry(string filePath,
System.IO.Compression.ZipArchive zipArchive)
{
var file = Path.GetFileName(filePath); // get file name in var file
zipArchive.CreateEntryFromFile(filePath, file);
}
var zip = "myFile.zip";
using (System.IO.Compression.ZipArchive archive = ZipFile.OpenRead(zip, ZipArchiveMode.Create))
{
AddEntry(file1, archive);
AddEntry(file2, archive);
}
var dirToExtract = "extract";
if (Directory.Exists(dirToExtract) == false)
{
Directory.CreateDirectory(dirToExtract);
}
ZipFile.ExtractToDirectory(zip, dirToExtract); //extracted file
using System.IO.Compression;
Console.WriteLine("-----------Zip - Unzip-----------");
static void AddEntry(string filePath,
System.IO.Compression.ZipArchive zipArchive)
{
var file = Path.GetFileName(filePath); // get file name in var file
zipArchive.CreateEntryFromFile(filePath, file);
}
var zip = "myFile.zip";
using (System.IO.Compression.ZipArchive archive = ZipFile.OpenRead(zip, ZipArchiveMode.Create))
{
AddEntry(file1, archive);
AddEntry(file2, archive);
}
var dirToExtract = "extract";
if (Directory.Exists(dirToExtract) == false)
{
Directory.CreateDirectory(dirToExtract);
}
ZipFile.ExtractToDirectory(zip, dirToExtract); //extracted file
Imports System.IO.Compression
Console.WriteLine("-----------Zip - Unzip-----------")
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'static void AddEntry(string filePath, System.IO.Compression.ZipArchive zipArchive)
'{
' var file = Path.GetFileName(filePath); ' get file name in var file
' zipArchive.CreateEntryFromFile(filePath, file);
'}
Dim zip = "myFile.zip"
Using archive As System.IO.Compression.ZipArchive = ZipFile.OpenRead(zip, ZipArchiveMode.Create)
AddEntry(file1, archive)
AddEntry(file2, archive)
End Using
Dim dirToExtract = "extract"
If Directory.Exists(dirToExtract) = False Then
Directory.CreateDirectory(dirToExtract)
End If
ZipFile.ExtractToDirectory(zip, dirToExtract) 'extracted file
In the above code, we have a ZIP file named myFile.zip, and we use system namespace. OpenRead
method is used to open the ZIP file in specified mode. This can also be used to create new ZIP archive files. Once opened we can add a new archive entry using the method AddEntry
and then ExtractToDirectory
extracts ZIP archive files or selected files to the specified directory. The directory is checked if exists. If it does not exist then a directory is created using Directory.CreateDirectory
. dirToExtract
is the directory path to extract ZIP archive files. The last line in the code ZipFile.ExtractToDirectory(zip, dirToExtract)
extracts a ZIP file. All the files are saved in the directory path.
Open Project Manager from Visual Studio and search for the IronZIP package. Then select the latest version and click Install. You can change the version to be installed from the drop-down. Then click Install.
Create a ZIP Archive File and add files using IronZIP
IronZIP is an archive compression and decompression library developed by Iron Software. In addition to the widely used ZIP format, it can also handle TAR, GZIP, and BZIP2.
IronZIP is a C# ZIP Archive Library that prioritizes accuracy, ease of use, and speed. User-friendly API enables developers to easily add archive functionality to modern .NET projects in minutes.
IronZIP has many advantages compared to System.Io.Compression
library. You can specify the compression ratio you want during compression and also use different compression algorithms ZIP, TAR, GZIP, BZIP2. It also supports mobile, web, and desktop platforms and various .NET versions.
IronZIP has class IronArchive
which can be used to create ZIP files, and extract ZIP files and also supports TAR, GZIP, and BZIP2 archive file creation using a single library. Below is the code to create a ZIP file. IronArchive
class creates/opens ZIP files and then archive.Add
method is used to add file entries to the archive like below.
// setup
var archivePath= "ironZip.zip";
if (File.Exists(archivePath))
{
File.Delete(archivePath);
}
// zip file
using (var archive = new IronZipArchive(9))
{
// Add files to the zip
archive.Add(file1);
archive.Add(file2);
archive.SaveAs(archivePath);
}
// setup
var archivePath= "ironZip.zip";
if (File.Exists(archivePath))
{
File.Delete(archivePath);
}
// zip file
using (var archive = new IronZipArchive(9))
{
// Add files to the zip
archive.Add(file1);
archive.Add(file2);
archive.SaveAs(archivePath);
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
The initial source code has the setup required to specify the ZIP archive file name and check if the specified directory exists. Then we archive the files to create a ZIP file using Add
method. The second parameter in the compression parameter is 1 for lower and 9 for higher. txt files can be compressed to max using 9 without loss and image files can use lower compression to avoid data loss.
IronArchive
class can also be used to extract the files from the ZIP archive file. All the files are extracted using IronArchive.ExtractArchiveToDirectory
that helps to extract all the files to specific directories like below.
var extractionPath = "IronZipFiles";
// extract files from zip
if (Directory.Exists(extractionPath) == false)
{
Directory.CreateDirectory(extractionPath);
}
IronArchive.ExtractArchiveToDirectory(archivePath, extractionPath);
var extractionPath = "IronZipFiles";
// extract files from zip
if (Directory.Exists(extractionPath) == false)
{
Directory.CreateDirectory(extractionPath);
}
IronArchive.ExtractArchiveToDirectory(archivePath, extractionPath);
Dim extractionPath = "IronZipFiles"
' extract files from zip
If Directory.Exists(extractionPath) = False Then
Directory.CreateDirectory(extractionPath)
End If
IronArchive.ExtractArchiveToDirectory(archivePath, extractionPath)
The above code extracts ZIP files to a directory. The code checks for the directory exist then the ZIP archive file is extracted to the specified directory.
For the above code to work a license key is required. This key needs to be placed in appsettings.json
"IronZip.LicenseKey": "your lincese key"
"IronZip.LicenseKey": "your lincese key"
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'"IronZip.LicenseKey": "your lincese key"
A trial license is available for developers up on registering here and yes, no credit card is required for a trial license. One can provide the email ID and register for a free trial.
Use the static FromFile
method from IronZIP to open an existing archive. This method also requires specifying the file name of the new archive that will be created as output.
//add to existing archive
const string file3 = ".\\image3.png";
var archivePlusPath = "ironZipPlus.zip";
using (var file = IronArchive.FromFile(archivePath, archivePlusPath))
{
// Add files
file.Add(file3);
}
//add to existing archive
const string file3 = ".\\image3.png";
var archivePlusPath = "ironZipPlus.zip";
using (var file = IronArchive.FromFile(archivePath, archivePlusPath))
{
// Add files
file.Add(file3);
}
'add to existing archive
Const file3 As String = ".\image3.png"
Dim archivePlusPath = "ironZipPlus.zip"
Using file = IronArchive.FromFile(archivePath, archivePlusPath)
' Add files
file.Add(file3)
End Using
The code uses the IronArchive.FromFile
static void method to open the existing ZIP file and then add the new file as an archive entry. Once the file is disposed then the file is successfully saved.
The IronZIP library is constantly updated based on feedback from customers/users and all the updates can be found here.
In conclusion, ZIP file programming is an essential skill to build in modern application development where storage and data transfer costs are charged by cloud host providers. Knowing this skill can help programmers reduce the cost of the application and improve the performance of the application.
By following the installation steps and exploring the provided code examples, developers can quickly leverage the power of IronZIP to make new files and handle Zip/Unzip tasks with ease. As more and more applications are modernized and moved to the cloud, having a reliable ZIP library like IronZIP equips C# developers with the tools necessary to meet the demands of modern application development. So, embrace the power of IronZIP and unlock new possibilities for working with ZIP files in your C# applications.
IronZIP offers extensive support for its developers. To know more about how IronZIP for C# works visit here. IronZIP offers a free trial license which is a great opportunity to know IronZIP and its features.
Iron Software has various other libraries, explore them to gain more knowledge and update your skills to program/develop modern applications.
9 .NET API products for your office documents