How to Zip Files in .NET with C#
Why Use Zip Files with C#?
The pdfRest Zip Files API Tool offers a convenient way to programmatically zip multiple files together. This tutorial will demonstrate how to send an API call to the Zip Files endpoint using C# to leverage this functionality.
The ability to zip files through an API call is particularly useful when dealing with multiple documents that need to be compressed into a single package, making them easier to manage, store, or transmit.
In the real world, a user might need to zip files to reduce the overall file size for storage efficiency or to ensure faster transmission over the internet. For instance, a business could use this functionality to bundle various reports and documents into a single, compressed file before sending it to a client, thus saving bandwidth and improving the client's experience by simplifying the number of downloads they must manage.
Zip Files with C# Code Example
The following code is a complete example of making an API call to the Zip Files endpoint using C#. The code is sourced from the pdfRest API samples available on GitHub: pdf-rest-api-samples.
using System.Text; using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "zip")) { request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); request.Headers.Accept.Add(new("application/json")); var multipartContent = new MultipartFormDataContent(); var byteArray = File.ReadAllBytes("/path/to/file.pdf"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.pdf"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); var byteArray2 = File.ReadAllBytes("/path/to/file.pdf"); var byteAryContent2 = new ByteArrayContent(byteArray2); multipartContent.Add(byteAryContent2, "file", "file_name.pdf"); byteAryContent2.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("API response received."); Console.WriteLine(apiResult); } }
Breaking Down the Code
The code above illustrates how to create a multipart/form-data request to zip files using the pdfRest API. Let's break down the key components:
var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }
This line initializes a new HttpClient
instance with the base address set to the pdfRest API.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
This line adds your API key to the request headers. Replace "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" with your actual pdfRest API key.
var multipartContent = new MultipartFormDataContent();
A MultipartFormDataContent
object is created to hold the files that will be zipped together.
var byteArray = File.ReadAllBytes("/path/to/file.pdf"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.pdf"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
These lines read the contents of a file into a byte array, wrap it in a ByteArrayContent
object, and add it to the multipart content. The "file_name" should be replaced with the actual name of the file. The Content-Type
header is set to "application/pdf" as this example assumes PDF files are being zipped.
Repeat this code for each file that you wish to add to the Zip file.
var response = await httpClient.SendAsync(request);
This line sends the multipart request asynchronously and waits for the response.
var apiResult = await response.Content.ReadAsStringAsync();
Finally, the response content is read as a string, which contains the API result.
Beyond the Tutorial
By following this tutorial, you've learned how to make a multipart API call to the pdfRest Zip Files endpoint using C#. This allows you to programmatically compress multiple files into a single zip archive. You're encouraged to explore and demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for further details on other API functionalities.
Note: This is an example of a multipart API call. Code samples using JSON payloads for other endpoints can be found at pdf-rest-api-samples.