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

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

    This topic demonstrates how to remove items from a GZipped Tar archive by retrieving a reference to a file contained within. Note: Tar and FileSystem-based GZip are not currently available in Xceed's .NET Compact Framework products.

    Basic steps

    To remove an item from a GZipped Tar archive, the following steps must be performed:

    When performing many operations on an archive, you may wish to consider optimizing your code by using batch updates

    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.

    Demonstration

    This example demonstrates how to remove a file from within a GZipped Tar archive.

    C#
    Copy Code
    public static void Example1()
    {
      // Select the output Tar-GZip file on disk
      AbstractFile tgzFile = new DiskFile( "Example.tar.gz" );
    
      // If the 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 );
    
        // Batch the following operations in the Tar archive
        using( AutoBatchUpdate batch = new AutoBatchUpdate( tar ) )
        {
          /* Example 1: Delete a specific file */
          // Select a file in the archive
          AbstractFile file = tar.GetFile( @"\Folder1\Folder2\File3.dat" );
    
          // If the file exists
          if( file.Exists )
          {
            // Delete it
            file.Delete();
          }
    
          /* Example 2: Delete a specific folder */
          // Select a folder in the archive
          AbstractFolder folder = tar.GetFolder( @"\Folder1\Folder2" );
    
          // If the folder exists
          if( folder.Exists )
          {
            // Delete it and all items within
            folder.Delete();
          }
        }
      }
    }
    VB.NET
    Copy Code
    Public Shared Sub Example1()
        ' Select the output Tar-GZip file on disk
        Dim tgzFile As AbstractFile = New DiskFile("Example.tar.gz")
    
        ' If the 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)
    
            ' Batch the following operations in the Tar archive
            Using batch As New AutoBatchUpdate(tar)
                ' Example 1: Delete a specific file 
                ' Select a file in the archive
                Dim file As AbstractFile = tar.GetFile("\Folder1\Folder2\File3.dat")
    
                ' If the file exists
                If file.Exists Then
                    ' Delete it
                    file.Delete()
                End If
    
                ' Example 2: Delete a specific folder 
                ' Select a folder in the archive
                Dim folder As AbstractFolder = tar.GetFolder("\Folder1\Folder2")
    
                ' If the folder exists
                If folder.Exists Then
                    ' Delete it and all items within
                    folder.Delete()
                End If
            End Using
        End If
    End Sub

    This example demonstrates how to remove a file from within a non-GZipped Tar archive.

    C#
    Copy Code
    public static void Example2()
    {
      // Select the output Tar file on disk
      AbstractFile tarFile = new DiskFile( "Example.tar" );
    
      // If the Tar file exists
      if( tarFile.Exists )
      {
        // Wrap the tar file around a logical TarArchive object
        TarArchive tar = new TarArchive( tarFile );
    
        // Batch the following operations in the Tar archive
        using( AutoBatchUpdate batch = new AutoBatchUpdate( tar ) )
        {
          /* Example 1: Delete a specific file */
          // Select a file in the archive
          AbstractFile file = tar.GetFile( @"\Folder1\Folder2\File3.dat" );
    
          // If the file exists
          if( file.Exists )
          {
            // Delete it
            file.Delete();
          }
    
          /* Example 2: Delete a specific folder */
          // Select a folder in the archive
          AbstractFolder folder = tar.GetFolder( @"\Folder1\Folder2" );
    
          // If the folder exists
          if( folder.Exists )
          {
            // Delete it and all items within
            folder.Delete();
          }
        }
      }
    }
    VB.NET
    Copy Code
    Public Shared Sub Example2()
        ' Select the output Tar file on disk
        Dim tarFile As AbstractFile = New DiskFile("Example.tar")
    
        ' If the Tar file exists
        If tarFile.Exists Then
            ' Wrap the tar file around a logical TarArchive object
            Dim tar As New TarArchive(tarFile)
    
            ' Batch the following operations in the Tar archive
            Using batch As New AutoBatchUpdate(tar)
                ' Example 1: Delete a specific file 
                ' Select a file in the archive
                Dim file As AbstractFile = tar.GetFile("\Folder1\Folder2\File3.dat")
    
                ' If the file exists
                If file.Exists Then
                    ' Delete it
                    file.Delete()
                End If
    
                ' Example 2: Delete a specific folder 
                ' Select a folder in the archive
                Dim folder As AbstractFolder = tar.GetFolder("\Folder1\Folder2")
    
                ' If the folder exists
                If folder.Exists Then
                    ' Delete it and all items within
                    folder.Delete()
                End If
            End Using
        End If
    End Sub

    Things you should consider

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