How to Easily Create a Complex Zip Archive in C# .NET Core

Cloudmersive
3 min readMay 30, 2024

--

Creating and extracting zip archives is an everyday task for many of us. Automating the compression of complex archives can save a lot of time.

We can think of a “complex” zip archive as one that contains multiple directories and subdirectories, rather than just a simple collection of compressed files. Adding file structure to an archive is straightforward when we create one manually, but it’s a bit more complex to automate that type of archive creation with code.

Thankfully, using C# .NET Core code examples provided further down the page, we can take advantage of a free API that makes complex archive compression much more straightforward. We can create custom archives by constructing our input request like the following JSON request model:

{
"FilesInZip": [
{
"FileName": "string",
"FileContents": "string"
}
],
"DirectoriesInZip": [
{
"DirectoryName": "string",
"DirectoriesInDirectory": [
null
],
"FilesInDirectory": [
{
"FileName": "string",
"FileContents": "string"
}
]
}
]
}

This way, we’ll be able to easily create archives with organized directories and subdirectories. This is particularly advantageous if we tend to create archives in a similar format for common tasks (such as sending compressed application code across networks, for example).

To authorize our API calls, we’ll just need a free Cloudmersive API key. This will allow us to make up to 800 API calls per month with no additional commitments (our total will simply reset the following month once we reach our limit).

Our first step is to install the client SDK via NuGet. Let’s run the following command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1

Next, let’s use the below examples to add our namespace imports and call our archive function. We can replace the “YOUR_API_KEY” placeholder text with our own API key string:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class ZipArchiveZipCreateAdvancedExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ZipArchiveApi();
var request = new CreateZipArchiveRequest(); // CreateZipArchiveRequest | Input request

try
{
// Compress files and folders to create a new zip archive with advanced options
Object result = apiInstance.ZipArchiveZipCreateAdvanced(request);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ZipArchiveApi.ZipArchiveZipCreateAdvanced: " + e.Message );
}
}
}
}

If we want to go a step further and secure our complex zip archive with a password, we can handle that using a different API with the same SDK. We can use the following code, which allows us to set a password AND specify the encryption algorithm we want:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class ZipArchiveZipEncryptAdvancedExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ZipArchiveApi();
var encryptionRequest = new ZipEncryptionAdvancedRequest(); // ZipEncryptionAdvancedRequest | Encryption request

try
{
// Encrypt and password protect a zip file
Object result = apiInstance.ZipArchiveZipEncryptAdvanced(encryptionRequest);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ZipArchiveApi.ZipArchiveZipEncryptAdvanced: " + e.Message );
}
}
}
}

And that’s all there is to it — now we can easily create complex zip archives AND secure them using just a few lines of C# code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet