How to create TAR file in .NET 7 using C#


In this article, we are going to see how to create TAR files and extract files from TAR files in .NET 7 using C#

For this, I have created 2 folders with the following names

1) SourceFiles : This folder contains files that we want to include in the TAR file. For the demo, I have kept 2 txt files.

2) ExtractFiles: This folder is being to extract files from the TAR files which we have created using the SourceFiles folder through C#

Image 1: Folder structure for the demo

Image 2: Folder structure of the SourceFiles Folder

Now we are going to write code in Visual Studio 2022 to create TAR file

Step 1

Open Visual Studio 2022. For this demonstration, I’m using Visual Studio 2022, version 17.4.1, which you can use this version or higher if you want to have.NET 7 support in Visual Studio.

Select Create a new project.

Step 2

Search Console App 

Select  Console App (make sure you choose the Console App not Console App .Net Framework as it is a older version of .NET) 

Click “Next.”

Step 3

Provide the name in Project Name.

Click “Next.”

Step 4

In this step, you have to select the framework.

Select .NET 7.0

Click “Next.”

Step 5

Now in this step, you have to write the code for creating a TAR file from the folder.

In the CreateFromDirectory method, you have to provide three arguments.

  1. Source folder path
  2. Destination path with the file name 
  3. Whether you have to include base directory or you have to create TAR file for the only the files that are included in the folder

Here is the code for your convenience

using System.Formats.Tar;

// Code for creating TAR file from folder
TarFile.CreateFromDirectory(
    @"D:\TARFileDemo\SourceFiles",
    @"D:\TARFileDemo\Demo.tar",
    false);

Step 6

After writing the above code, run the application.

After running the application, you can see in the folder that a TAR file is created with the same name that you have mentioned in the destination folder.

Step 7

Now in this step, we are going to see how to extract files from the TAR file that we have created.

For that, you have to use the ExtractToDirectory method, in which you have to provide three arguments.

  1. Source TAR file path from which we have to extract the files
  2. Destination folder where we have to extract the files
  3. In this you have to choose that you want to overwrite the existing file or not

Here is the code for your convenience

//Code to extract files from TAR file
TarFile.ExtractToDirectory(
    @"D:\TARFileDemo\Demo.tar",
    @"D:\TARFileDemo\ExtractFiles",
    false);

Step 8 

After writing the above code, run the application.

After running the application, you can see in the destination folder that all files were extracted from TAR files.

If you like this article also have a look @ .NET 7 Overview article where you can find the Changes and Support Cycle of it.

If you have Comment or Suggestion please let me know in the comment section or mail me @ [email protected]


You may also like...

1 Response

  1. December 2, 2022

    […] 7) Tar file creation […]

Leave a Reply

Your email address will not be published. Required fields are marked *