This topic demonstrates how to copy files and folders to a GZipped Tar archive using a disk folder as the source where the files to process are located.
To copy items to a Tar archive, the following steps must be performed:
Retrieve a reference to a folder whose files will be added to the Tar archive, using a DiskFolder, ZipArchive, MemoryFolder or IsolatedFolder class. With Xceed's FileSystem-based products, a folder is a folder: it does not matter if it is located within a Tar archive, on disk or in memory.
Retrieve a reference to an AbstractFile-derived class (such as DiskFile), using the name of the Tar archive.
Retrieve a reference to a GZip file using the GZippedFile class, passing the AbstractFile-based object retrieved in the previous step to its constructor. Note: To create a non-GZipped Tar archive, omit this step.
Retrieve a reference to a new or existing Tar archive using the TarArchive class, passing the GZippedFile retrieved in the previous step to its constructor (or the DiskFile object from the first step for a non-GZipped Tar archive).
Call the CopyFilesTo method to copy the entire contents of the folder to the Tar archive.
Using a non-GZipped Tar archive may be preferable in some situations, for example, when read/write speed is essential but storage space is not, when you need to access individual files in the Tar archive without uncompressing the entire archive, or when the files inside the Tar archive are already compressed.
Setting the LicenseKey property with Tar also unlocks the GZip capabilities.
When performing many operations on an archive, it is best to optimize the operations by using batch updates like the examples show.
This example demonstrates how to copy files from a disk folder to a GZipped Tar archive.
| C# |
Copy Code |
|---|---|
using Xceed.FileSystem; using Xceed.Tar; using Xceed.GZip; namespace FileSystemDocumentationExamples.TarComponent { public class CreateTarGZipArchive1 { static void Example() { Xceed.Tar.Licenser.LicenseKey = "<your ZIN- license key>"; /* There is no need to set the GZip license. A Tar unlocks GZip automatically */ //Xceed.GZip.Licenser.LicenseKey = "<your ZIN license key>"; // Select the output Tar-GZip file on disk AbstractFile tgzFile = new DiskFile( "Example.tar.gz" ); // If the physical Tar-GZip file already exists if( tgzFile.Exists ) { // Delete it. We want to create a new archive, not update an existing one tgzFile.Delete(); } // Wrap the physical Tar-GZip around a logical GZippedFile object GZippedFile gzip = new GZippedFile( tgzFile, "Example.tar" ); // Wrap the logical GZip file around a logical TarArchive object TarArchive tar = new TarArchive( gzip ); // Select a source folder AbstractFolder sourceFolder = new DiskFolder( @"D:\Data" ); // Batch the following operations into the Tar archive using( AutoBatchUpdate batch = new AutoBatchUpdate( tar ) ) { // Add the files from the source folder to the archive sourceFolder.CopyFilesTo( tar, true, true ); } } } } | |
| VB.NET |
Copy Code |
|---|---|
Imports Xceed.FileSystem Imports Xceed.Tar Imports Xceed.GZip Namespace FileSystemDocumentationExamples.TarComponent Public Class CreateTarGZipArchive1 Private Shared Sub Example() Xceed.Tar.Licenser.LicenseKey = "<your ZIN- license key>" ' There is no need to set the GZip license. A Tar unlocks GZip automatically 'Xceed.GZip.Licenser.LicenseKey = "<your ZIN license key>"; ' Select the output Tar-GZip file on disk Dim tgzFile As AbstractFile = New DiskFile("Example.tar.gz") ' If the physical Tar-GZip file already exists If tgzFile.Exists Then ' Delete it. We want to create a new archive, not update an existing one tgzFile.Delete() End If ' Wrap the physical Tar-GZip around a logical GZippedFile object Dim gzip As New GZippedFile(tgzFile, "Example.tar") ' Wrap the logical GZip file around a logical TarArchive object Dim tar As New TarArchive(gzip) ' Select a source folder Dim sourceFolder As AbstractFolder = New DiskFolder("D:\Data") ' Batch the following operations into the Tar archive Using batch As New AutoBatchUpdate(tar) ' Add the files from the source folder to the archive sourceFolder.CopyFilesTo(tar, True, True) End Using End Sub End Class End Namespace | |
This example demonstrates how to copy files from a disk folder to a non-GZipped Tar archive.
| C# |
Copy Code |
|---|---|
// Select the output Tar file on disk AbstractFile tarFile = new DiskFile( "Example.tar" ); // If the physical Tar file already exists if( tarFile.Exists ) { // Delete it. We want to create a new archive, not update an existing one tarFile.Delete(); } // Wrap the physical tar file around a logical TarArchive object TarArchive tar = new TarArchive( tarFile ); // Select a source folder AbstractFolder sourceFolder = new DiskFolder( @"D:\Data" ); // Batch the following operations into the Tar archive using( AutoBatchUpdate batch = new AutoBatchUpdate( tar ) ) { // Add the files from the source folder to the archive sourceFolder.CopyFilesTo( tar, true, true ); } | |
| VB.NET |
Copy Code |
|---|---|
' Select the output Tar file on disk Dim tarFile As AbstractFile = New DiskFile("Example.tar") ' If the physical Tar file already exists If tarFile.Exists Then ' Delete it. We want to create a new archive, not update an existing one tarFile.Delete() End If ' Wrap the physical tar file around a logical TarArchive object Dim tar As New TarArchive(tarFile) ' Select a source folder Dim sourceFolder As AbstractFolder = New DiskFolder("D:\Data") ' Batch the following operations into the Tar archive Using batch As New AutoBatchUpdate(tar) ' Add the files from the source folder to the archive sourceFolder.CopyFilesTo(tar, True, True) End Using | |
The main questions you should ask yourself when copying items to a Tar archive are:
Do you want to copy a file rather than a folder or its contents? Create a DiskFile class rather than a DiskFolder class.
Do you want to filter (specify specific files and folders) the items that are to be added to the Tar archive? Use filters.
Do you want to display the status of the operation? See the Events topic.
Do you want to add items into a specific folder within the Tar archive file? Create an instance of a TarredFolder object rather than a TarArchive object.
Do you only want to copy a specific file or folder? Use the CopyTo method.
Do you want to move items rather than copy them? Use the MoveTo and MoveFilesTo methods.
Do you want to change the location of the temporary folder? Set the TarArchive's TempFolder property.