How to Unzip Files in .NET with C#

Learn how to unzip files from .zip archives with C# using the pdfRest Zip Files API Tool
Share this page

Why Unzip Files with C#?

The pdfRest Zip Files API Tool provides a powerful and efficient way to handle file unzipping operations through API calls. By leveraging this tool, developers can automate the process of unzipping files directly from their applications using C#. This tutorial will guide you through the steps of sending an API call to unzip files using C#, showcasing how to integrate this functionality seamlessly into your applications.

Imagine a scenario where you are developing an application that handles large volumes of documents received in compressed formats. You might need to extract these documents from a zip file to process them individually. Using the pdfRest Zip Files API Tool, you can automate this extraction process, ensuring that your application remains efficient and responsive.

Unzip Files with C# Code Example

using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
    using (var request = new HttpRequestMessage(HttpMethod.Post, "unzip"))
    {
        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");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name");
        byteAryContent.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);
    }
}

Source: GitHub Repository

Breaking Down the Code

The example begins by setting up an HttpClient with a base address pointing to the pdfRest API endpoint: https://api.pdfrest.com. This client is used to send HTTP requests.

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })

Next, an HttpRequestMessage is created to send a POST request to the "unzip" endpoint. This endpoint is responsible for handling the zipping operation.

using (var request = new HttpRequestMessage(HttpMethod.Post, "unzip"))

The request headers are configured to include an API key, which is necessary for authentication. The API key should be replaced with a valid key obtained from your pdfRest account.

request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

The request accepts JSON responses, as indicated by setting the Accept header.

request.Headers.Accept.Add(new("application/json"));

A MultipartFormDataContent object is created to hold the file data. The file is read into a byte array from the specified path and added to the multipart content with a name and file name.

var byteArray = File.ReadAllBytes("/path/to/file");
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", "file_name");

The Content-Type header is set to "application/pdf" to specify the type of file being sent.

byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

The request content is set to the multipart content, and the request is sent asynchronously. The response is read as a string and printed to the console.

var response = await httpClient.SendAsync(request);
var apiResult = await response.Content.ReadAsStringAsync();
Console.WriteLine("API response received.");
Console.WriteLine(apiResult);

Beyond the Tutorial

In this tutorial, you learned how to use C# to send an API call to the pdfRest Zip Files API Tool. This example demonstrated how to set up the necessary headers, create a multipart form data request, and handle the response. By following these steps, you can integrate file zipping capabilities into your applications.

To explore more, you can demo all of the pdfRest API Tools in the API Lab. Additionally, refer to the API Reference Guide for detailed documentation on all available endpoints and parameters.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub Repository.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.