How to Compress Files and Folders to Create a New Zip Archive using C# .NET Framework

Cloudmersive
2 min readJan 24, 2024

--

Zip archives allow us to compress large quantities of documents — or even large groups of folders — into a small & extremely manageable file. With a low-code API solution, we can reduce the effort it takes to generate zip archives in our C# .NET applications.

Using the ready-to-run code examples below, we can take advantage of a free API that allows us to compress files and folders in a new zip archive. We can leverage advanced options to control the structure of the resulting zip archive by structuring our request like so:

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

To make our API calls, we’ll need to start by grabbing a free API key. These allow a limit of 800 API calls per month with no additional commitments.

After that, we can install the SDK. To install via NuGet, let’s run the below command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2

Finally, let’s call the function using the below code, making sure to include our input request parameters & API key in their respective lines:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.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 );
}
}
}
}

Just like that, we can easily create complex zip archives with a single low-code API call.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet