[Root] / Xceed Data Manipulation Components for .NET / [Root] / Basic Concepts / [Root] / Tar and GZip capabilities / [Root] / Xceed Tar / Copying items to a tar archive

In This Topic
    Copying items to a tar archive
    In This Topic

    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.

    Basic steps

    To copy items to a Tar archive, the following steps must be performed:

    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.

    Demonstration

    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

    Things you should consider

    The main questions you should ask yourself when copying items to a Tar archive are:

    See Also