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

In This Topic
    Extracting items from a tar archive
    In This Topic

    This topic demonstrates how to extract files and folders from a GZipped Tar archive to a destination folder. 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.

    Basic steps

    To extract items from a Tar archive, the following steps must be performed:

    • Retrieve a reference to an AbstractFile-derived class representing the actual Tar archive file; in this example, DiskFile will be used. If the Tar archive is GZipped, retrieve a reference to a GZippedFile class and pass the DiskFile object you have just retrieved to its constructor. 

    • Retrieve a reference to a new or existing Tar archive using the TarArchive class, using either the GZippedFile object you have just retrieved for a GZipped Tar archive or a DiskFile object for a non-GZipped Tar archive. 

    • Retrieve a reference to a folder where the files will be extracted to using an AbstractFolder-derived class such as DiskFolder, ZipArchive, MemoryFolder or IsolatedFolder. 

    • Call the CopyFilesTo method to copy the entire contents of the Tar archive to the destination folder.

    Demonstration

    This example demonstrates how to extract items from a Tar archive to a folder located on disk.

    C#
    Copy Code
    using Xceed.FileSystem;
    using Xceed.Tar;
    using Xceed.GZip;
    
    namespace FileSystemDocumentationExamples.TarComponent
    {
      public class ExtractTarGZipArchive1
      {
        static void ExtractExample()
        {
          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 exists
          if( tgzFile.Exists )
          {
            // 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 destination folder
            AbstractFolder destfolder = new DiskFolder( @"D:\Home" );
    
            // Extract all the files from the archive
            tar.CopyFilesTo( destfolder, true, true );
          }
        }
      }
    }
    VB.NET
    Copy Code
    Imports Xceed.FileSystem
    Imports Xceed.Tar
    Imports Xceed.GZip
    
    Namespace FileSystemDocumentationExamples.TarComponent
        Public Class ExtractTarGZipArchive1
            Private Shared Sub ExtractExample()
                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 exists
                If tgzFile.Exists Then
                    ' 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 destination folder
                    Dim destfolder As AbstractFolder = New DiskFolder("D:\Home")
    
                    ' Extract all the files from the archive
                    tar.CopyFilesTo(destfolder, True, True)
                End If
            End Sub
        End Class
    End Namespace

    This example demonstrates how to track the extraction operations using events.

    C#
    Copy Code
    public class ExtractTarGZipArchive2
    {
      void ExtractExample2()
      {
        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 exists
        if( tgzFile.Exists )
        {
          // Create a FileSystemEvents object to ac on events during operations
          TarEvents events = new TarEvents();
          object userData = null;
    
          // Handle the ReadingTarItemProgression event
          events.ReadingTarItemProgression += Events_ReadingTarItemProgression;
    
          // Handle the ItemProgression event
          events.ItemProgression += Events_ItemProgression;
    
          // Handle the ByteProgression event
          events.ByteProgression += Events_ByteProgression;
    
          // 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( events, userData, gzip );
    
          // Select a destination folder
          AbstractFolder destfolder = new DiskFolder( @"D:\Home" );
    
          // Extract all the files from the archive
          tar.CopyFilesTo( events, userData, destfolder, true, true );
        }
      }
    
      private void Events_ReadingTarItemProgression( object sender, ItemProgressionEventArgs e )
      {
        /* Raised when a Tar archive is being read for the first time or being refreshed, providing progression information to the event handler.
           Typically, the event is raised in the TarArchive constructor and during a call to Refresh(). */
    
        byte perentComplete = e.AllItems.Percent;
      }
    
      private void Events_ByteProgression( object sender, ByteProgressionEventArgs e )
      {
        long currentFileNumBytesProcessed = e.CurrentFileBytes.Processed;
        long currentFileTotalBytes = e.CurrentFileBytes.Total;
        byte currentFilePercentComplete = e.CurrentFileBytes.Percent;
    
        long numTotalBytesProcessed = e.AllFilesBytes.Processed;
        long totalBytes = e.AllFilesBytes.Total;
        byte totalPercentComplete = e.AllFilesBytes.Percent;
      }
    
      private void Events_ItemProgression( object sender, ItemProgressionEventArgs e )
      {
        long numProcessed = e.AllItems.Processed;
        long totalItems = e.AllItems.Total;
        byte percentComplete = e.AllItems.Percent;
    
        string extractingName = e.CurrentItem.FullName;
      }
    }
    VB.NET
    Copy Code
    Public Class ExtractTarGZipArchive2
        Private Sub ExtractExample2()
            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 exists
            If tgzFile.Exists Then
                ' Create a FileSystemEvents object to ac on events during operations
                Dim events As New TarEvents()
                Dim userData As Object = Nothing
    
                ' Handle the ReadingTarItemProgression event
                AddHandler events.ReadingTarItemProgression, AddressOf Events_ReadingTarItemProgression
    
                ' Handle the ItemProgression event
                AddHandler events.ItemProgression, AddressOf Events_ItemProgression
    
                ' Handle the ByteProgression event
                AddHandler events.ByteProgression, AddressOf Events_ByteProgression
    
                ' 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(events, userData, gzip)
    
                ' Select a destination folder
                Dim destfolder As AbstractFolder = New DiskFolder("D:\Home")
    
                ' Extract all the files from the archive
                tar.CopyFilesTo(events, userData, destfolder, True, True)
            End If
        End Sub
    
        Private Sub Events_ReadingTarItemProgression(ByVal sender As Object, ByVal e As ItemProgressionEventArgs)
            '       Raised when a Tar archive is being read for the first time or being refreshed, providing progression information to the event handler.
            '         Typically, the event is raised in the TarArchive constructor and during a call to Refresh(). 
    
            Dim perentComplete As Byte = e.AllItems.Percent
        End Sub
    
        Private Sub Events_ByteProgression(ByVal sender As Object, ByVal e As ByteProgressionEventArgs)
            Dim currentFileNumBytesProcessed As Long = e.CurrentFileBytes.Processed
            Dim currentFileTotalBytes As Long = e.CurrentFileBytes.Total
            Dim currentFilePercentComplete As Byte = e.CurrentFileBytes.Percent
    
            Dim numTotalBytesProcessed As Long = e.AllFilesBytes.Processed
            Dim totalBytes As Long = e.AllFilesBytes.Total
            Dim totalPercentComplete As Byte = e.AllFilesBytes.Percent
        End Sub
    
        Private Sub Events_ItemProgression(ByVal sender As Object, ByVal e As ItemProgressionEventArgs)
            Dim numProcessed As Long = e.AllItems.Processed
            Dim totalItems As Long = e.AllItems.Total
            Dim percentComplete As Byte = e.AllItems.Percent
    
            Dim extractingName As String = e.CurrentItem.FullName
        End Sub
    End Class

    Things you should consider

    The main questions you should ask yourself when extracting items from a Tar archive are:

    • Do you want to filter (specify specific files and folders) the items that are to be extracted? Use filters. 

    • Do you want to display the status of the operation? See the Events topic. 

    • 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.