Create TAR Archives in C#

引入

塔尔 (Tape Archive) is a widely recognized format used in Unix-like systems for storing multiple files in a single archive. In this guide, you’ll learn how to create TAR files in C# using Aspose.ZIP for .NET, a robust library designed for efficient TAR archive management, including the option to create GZ format archives for enhanced compression.

為什麼要使用TAR?

使用 TAR 文件提供了几个好处:

  • 有效存储和包装文件 在一个档案中。
  • **在Linux和Unix系统中常用。
  • 支持额外的压缩格式 如 GZ 和 XZ。

内容表

设置 TAR 文件处理在 C#

要开始编程创建 TAR 档案,您将需要使用 Aspose.ZIP for .NET

  • 自动创建 TAR 档案
  • 支持各种压缩格式包括TAR、ZIP、GZ等。
  • 有效处理大文件

安装

您可以通过 NuGet 轻松安装图书馆,下列命令:

PM> Install-Package Aspose.Zip

Alternatively, you can download the DLL directly from the Aspose 下载页面.

2. 步骤创建 TAR 文件

遵循以下简单步骤 编程创建一个 TAR 档案:

  • Instantiate the TAR Archive: Create an instance of the TarArchive class.
  • Add Files to the Archive: Use the CreateEntry() method to include files and directories in the TAR.
  • Save the TAR File: Call the Save() method to store the archive.

代码例子

下面是一个快速的例子,展示如何创建一个TAR档案:

// Create a new TAR archive
using (TarArchive archive = new TarArchive())
{
    // Add files to the TAR archive
    archive.CreateEntry("file1.txt", "input/file1.txt");
    archive.CreateEntry("image.png", "input/image.png");

    // Save the TAR file
    archive.Save("output.tar");
}

此方法允许您将多个文件集成到一个 TAR 档案库中,您也可以创建 GZ 格式档案库,以进一步压缩文件。

保存TAR档案

添加文件后,您可以使用下列命令 保存 TAR 档案:

archive.Save("final.tar");

此步骤确保您有一个 压缩和精心结构的档案库 准备好使用。

处理多个文件在TAR档案

动态添加多个文件,您可以通过指定的文件夹滚动。

string[] files = Directory.GetFiles("input_folder");
using (TarArchive archive = new TarArchive())
{
    foreach (string file in files)
    {
        archive.CreateEntry(Path.GetFileName(file), file);
    }
    archive.Save("batch_archive.tar");
}

这种方法简化了 自动大规模TAR文件创建的过程,并在必要时允许轻松的GZ压缩集成。

5、获得免费的API许可证

要解锁 Aspose.ZIP 的完整功能,请考虑请求 (免费暂时许可证)(http://purchase.aspose.com/temporary-license)

要了解更多详细的文档,请参阅 Aspose.ZIP 指南 或与社区合作,在 **A Spose 论坛 您可能有任何查询。

结论和额外资源

总结

在此指南中,我们涵盖了:

  • 如何在C#中创建TAR文件
  • 添加多个文件到 TAR 档案
  • 有效保存 TAR 档案
  • 处理大批 TAR 文件创建

使用 Aspose.ZIP for .NET,您可以在您的应用程序中有效地 创建、提取和管理 TAR 档案. 今天开始 自动处理 TAR 文件,并记住您也可以 创建 GZ 格式档案 以获得额外的压缩选项!

More in this category