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 is an essential resource for developers aiming to manage and manipulate zip files within their applications. This tutorial will guide you through sending an API call to unzip files using C#. By leveraging the pdfRest API, you can seamlessly integrate file extraction capabilities into your C# applications, enhancing functionality and user experience.

A user might need to extract multiple files from a zip archive for efficient processing or analysis. For example, a web application that allows users to upload compressed files can use the Zip Files API to extract these documents, enabling quick access and reducing manual handling.

Unzip Files with C# Code Example

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace pdfRestExample
{
    class Program
    {
        private static readonly HttpClient client = new HttpClient();

        static async Task Main(string[] args)
        {
            var filePath = "path/to/your/file.zip";
            var apiKey = "your_api_key";

            using (var content = new MultipartFormDataContent())
            {
                content.Add(new StringContent(apiKey), "apikey");
                content.Add(new StreamContent(File.OpenRead(filePath)), "file", Path.GetFileName(filePath));

                var response = await client.PostAsync("https://api.pdfrest.com/unzip", content);
                var responseString = await response.Content.ReadAsStringAsync();

                Console.WriteLine(responseString);
            }
        }
    }
}

Source: GitHub

Breaking Down the Code

The provided code uses the HttpClient class to make an HTTP POST request to the pdfRest API. Here's a breakdown of the key components:

var filePath = "path/to/your/file.zip";

This line specifies the path to the zip file you want to upload and unzip. Ensure the file path is correct and accessible by your application.

var apiKey = "your_api_key";

Your unique API key is required to authenticate the request. Replace "your_api_key" with your actual API key.

content.Add(new StringContent(apiKey), "apikey");

This line adds the API key to the request's form data as a string content. According to the pdfRest Cloud API Reference Guide, the apikey parameter is mandatory for authenticating API requests.

content.Add(new StreamContent(File.OpenRead(filePath)), "file", Path.GetFileName(filePath));

This snippet adds the zip file to the request content. The StreamContent reads the file stream, and Path.GetFileName(filePath) extracts the file name to be sent with the request.

var response = await client.PostAsync("https://api.pdfrest.com/unzip", content);

This line sends the POST request to the https://api.pdfrest.com/unzip endpoint with the multipart form data, including the API key and file.

Beyond the Tutorial

In this tutorial, you learned how to use C# to send an API call to the pdfRest Zip Files API Tool, enabling you to unzip files programmatically. To further explore the capabilities of pdfRest, you can demo all of the API Tools in the API Lab and refer to the comprehensive API Reference Guide.

Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us