Add Files to TAR
The code example demonstrates how to open an existing TAR archive and export it to a new TAR file using IronZip.
IronZip offers the capability to modify existing TAR files, making it a versatile tool for archive management in C# projects. To do so, start by utilizing the FromFile
method, which grants access to the archive you wish to modify.
Once you've opened the archive, you can use the Add
method to add files to the existing archive. This powerful feature simplifies the process of updating and expanding your zip archives, making it easy to adapt to your project's evolving needs.
Finally, use the SaveAs
method to export the resulting TAR file.
using IronZip;
using System;
class Program
{
static void Main()
{
// Path to the existing TAR file
string existingTarPath = "path/to/your/existing/archive.tar";
// Path where the new TAR file will be saved
string newTarPath = "path/to/your/new/archive.tar";
// Open the existing TAR archive
using (var archive = IronZip.TarArchive.FromFile(existingTarPath))
{
// Add a new file to the archive (as an example)
string fileToAdd = "path/to/file/you/want/to/add.txt";
archive.Add(fileToAdd, "newfile.txt"); // Adds the file under the name "newfile.txt" in the archive
// Export the modified TAR to a new file
archive.SaveAs(newTarPath);
}
Console.WriteLine("TAR archive has been modified and saved as a new archive.");
}
}
using IronZip;
using System;
class Program
{
static void Main()
{
// Path to the existing TAR file
string existingTarPath = "path/to/your/existing/archive.tar";
// Path where the new TAR file will be saved
string newTarPath = "path/to/your/new/archive.tar";
// Open the existing TAR archive
using (var archive = IronZip.TarArchive.FromFile(existingTarPath))
{
// Add a new file to the archive (as an example)
string fileToAdd = "path/to/file/you/want/to/add.txt";
archive.Add(fileToAdd, "newfile.txt"); // Adds the file under the name "newfile.txt" in the archive
// Export the modified TAR to a new file
archive.SaveAs(newTarPath);
}
Console.WriteLine("TAR archive has been modified and saved as a new archive.");
}
}
Imports IronZip
Imports System
Friend Class Program
Shared Sub Main()
' Path to the existing TAR file
Dim existingTarPath As String = "path/to/your/existing/archive.tar"
' Path where the new TAR file will be saved
Dim newTarPath As String = "path/to/your/new/archive.tar"
' Open the existing TAR archive
Using archive = IronZip.TarArchive.FromFile(existingTarPath)
' Add a new file to the archive (as an example)
Dim fileToAdd As String = "path/to/file/you/want/to/add.txt"
archive.Add(fileToAdd, "newfile.txt") ' Adds the file under the name "newfile.txt" in the archive
' Export the modified TAR to a new file
archive.SaveAs(newTarPath)
End Using
Console.WriteLine("TAR archive has been modified and saved as a new archive.")
End Sub
End Class
Explanation:
Using IronZip: The
IronZip
library is used for handling TAR archives. Ensure you have the necessary package installed in your project.FromFile Method: This method is used to open an existing TAR file. Replace
existingTarPath
with the path of your TAR file.Add Method: Allows you to add new files to the opened archive. Specify the path of the file you want to add and the name it should have inside the archive.
- SaveAs Method: Exports the modified archive to a new file. Set
newTarPath
to the desired output path for your new TAR file.
This code snippet provides a straightforward way to modify and export TAR archives using IronZip in a C# application. Ensure file paths are correctly specified and the IronZip library is properly referenced in your project.