Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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-value 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.
Below is an example that takes in five files, zips them into one archive, and exports them after the operation. All examples in this article are encased in the static void main to avoid repeated code.
using IronZip;
// Create an empty ZIP
using (var archive = new IronZipArchive())
{
// Add files to the ZIP
archive.Add("./assets/image1.jpg");
archive.Add("./assets/image2.jpg");
archive.Add("./assets/image3.jpg");
// Export the ZIP
archive.SaveAs("output.zip");
}
using IronZip;
// Create an empty ZIP
using (var archive = new IronZipArchive())
{
// Add files to the ZIP
archive.Add("./assets/image1.jpg");
archive.Add("./assets/image2.jpg");
archive.Add("./assets/image3.jpg");
// Export the ZIP
archive.SaveAs("output.zip");
}
Imports IronZip
' Create an empty ZIP
Using archive = New IronZipArchive()
' Add files to the ZIP
archive.Add("./assets/image1.jpg")
archive.Add("./assets/image2.jpg")
archive.Add("./assets/image3.jpg")
' Export the ZIP
archive.SaveAs("output.zip")
End Using
We first import IronZip.
Then, using IronZip, we create an archive.
We then call `archive.add()` and add multiple file to the archive. Remember that passing the path names must be absolute paths, or the operation will fail to find the corresponding files.
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;
// Open an existing ZIP
using (var archive = IronZipArchive.FromFile("existing.zip"))
{
// Add files
archive.Add("./assets/image3.png");
archive.Add("./assets/image4.png");
// Export the ZIP
archive.SaveAs("result.zip");
}
using IronZip;
// Open an existing ZIP
using (var archive = IronZipArchive.FromFile("existing.zip"))
{
// Add files
archive.Add("./assets/image3.png");
archive.Add("./assets/image4.png");
// Export the ZIP
archive.SaveAs("result.zip");
}
Imports IronZip
' Open an existing ZIP
Using archive = IronZipArchive.FromFile("existing.zip")
' Add files
archive.Add("./assets/image3.png")
archive.Add("./assets/image4.png")
' Export the ZIP
archive.SaveAs("result.zip")
End Using
We first import IronZip.
Then, using IronZip, we create an archive, but this time, we import the existing zip file with `IronZipArchive.FromFile()`.
We then call `archive.add()` and add the desired files to the archive. Remember that passing the path names must be absolute paths, or the operation will fail to find the existing file. Alternatively, separate this step with a variable string filename for a single file for easier readability.
Finally, we call `archive.SaveAs()` and export the 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.
We can also use IronZip to extract files from a Zip archive or Zip package, as in the examples above; let's review it in another example.
using IronZip;
// Extract ZIP
IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted");
using IronZip;
// Extract ZIP
IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted");
Imports IronZip
' Extract ZIP
IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted")
We import IronZip and call `IronZipArchive.ExtractArchieveToDirectory`; the method does exactly what is labeled and extracts merely the contents of the existing zip archive to the directory provided.
Furthermore, in cases where you would have to deal with password-protected zip archives, we could also use another method to extract the archives.
using IronZip;
using IronZip.Enum;
// Extract protected ZIP
IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd");
using IronZip;
using IronZip.Enum;
// Extract protected ZIP
IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd");
Imports IronZip
Imports IronZip.Enum
' Extract protected ZIP
IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd")
Ultimately, we pass in another parameter: the password to the protected zip archive. The rest of the operation is the same as the code above.
The code examples adding, extracting, and creating above are the most common when dealing with zip archives. However, in other cases where there needs to be more advanced usage, such as extracting other formats or simply viewing the archive context, IronZip has you covered and more.
Cross-compatibility: IronZip is compatible with a wide range of .NET versions, including .NET Core (8.7, 6.5, and 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 truly transcend the limitations of platforms and versions.
Generating Archives: The examples above mainly focus on the ZIP archive format; however, IronZip also works with TAR, GZIP, and BZIP2.
Extracting Archives: Extract archives and decompress files with IronZip with a single block of code, as shown above.
Adding Files and File Systems: IronZip also aids you in manipulating zip archive to the fullest, adding images, text files, documents such as PDFs, DOCX, and XLSX, and audio such as MP3 and WAV. You can even add an entire compressed file system or a compressed text file to create a ZipArchive archive.
Export and Create: In addition to creating an essential Zip archive, you can password-protect it with traditional or AES128 and AES256 standards. You can also generate and export formats such as TAR, GZIP, and BZIP2, which are mentioned above.
Custom Compression level: IronZip has custom compression files and allows developers to change the settings of the compression algorithm to fine-tune it to their needs.
Editing Archives: Add File entries, extract archives, and delete file entries easily using IronZip, a one-stop solution for all editing-related operations with archives.
File Entry Properties: IronZip allows you to set an optional archive comment and get file names of the files within the archive without extracting them, which helps leave specified comments for each file.
Dealing with compressed files and archives is a daily task that developers all have to do, but dealing with a large volume of them can be stressful and cause human-prone errors. Although there are options such as System.IO.Compression, using IronZip, you can achieve more and quickly resolve your problems when dealing with compressed archives. Within the article, we briefly discuss the advanced features of IronZip and basic operations that can commonly occur in daily scenarios, whether creating a zip archive, deleting entries within the zip archive, or adding in more files. Understanding how to do them programmatically with IronZip leads to increased efficiency and scalability and automating all akes related to archives.
10 .NET API products for your office documents