[Root] / Xceed Data Manipulation Components for .NET / [Root] / Basic Concepts / [Root] / Tar and GZip capabilities / [Root] / Xceed GZip / Multi-file operations

In This Topic
    Multi-file operations
    In This Topic

    This topic demonstrates how to handle multiple files in a GZip archive: listing its contents and adding an item.

    Although Xceed’s implementation of GZip allows you to read and create multi-file GZip archives, it is not recommended to store multiple files in a GZip archive, for two reasons. First, most popular archiving applications do not fully support multi-file GZip archives. Second, GZip archives possess no central header to indicate where each compressed file is stored, so when you extract a specific file from a GZip archive, each GZipped file preceding that file must first be decompressed. As a result, multi-file GZip archives can be very inefficient. It is strongly recommended that you combine the compression capabilities of GZip with the archiving capabilities of Tar instead. See the topics in the Xceed Tar book for details.

    To handle multi-file GZip archives, set the static boolean property AllowMultipleFiles of the GZipArchive class to true. By default, this property is set to false. If you attempt to create a multi-file GZip archive when this property is set to false, an exception will be thrown. If you attempt to read a multi-file GZip archive when this property is false, only the first file will be read.

    Basic steps

    To list the contents of a multi-file GZip archive, the following steps must be performed:

    To create a multi-file GZip archive, the following steps must be performed:

    You should always pass false (C#) or False (VB.NET) to the recursive parameter of the CopyFilesTo method when copying to a GZipArchive (the same applies to using the MoveFilesTo method with a GZipArchive), because the GZip format does not allow subfolders. If you set the recursive parameter to true/True, and if there are subfolders present in the folder you are attempting to copy or move, an exception will be thrown.

    Demonstration

    This example demonstrates how to list the contents of a multi-file GZip archive.

    C#
    Copy Code
    public static void ReadExample()
    {
      // This static property must be set before the GZipArchive is created.
      GZipArchive.AllowMultipleFiles = true;
    
      // Select a GZip file
      AbstractFile gzipFile = new DiskFile( @"D:\test.gz" );
    
      // If the GZip file exists
      if( gzipFile.Exists )
      {
        // Wrap the physical GZip around a logical GZipArchive object
        GZipArchive gzip = new GZipArchive( gzipFile );
    
        foreach( AbstractFile file in gzip.GetFiles( true ) )
        {
          Console.WriteLine( file.FullName );
        }
      }
    }
    VB.NET
    Copy Code
    Public Shared Sub ReadExample()
        ' This static property must be set before the GZipArchive is created.
        GZipArchive.AllowMultipleFiles = True
    
        ' Select a GZip file
        Dim gzipFile As AbstractFile = New DiskFile("D:\test.gz")
    
        ' If the GZip file exists
        If gzipFile.Exists Then
            ' Wrap the physical GZip around a logical GZipArchive object
            Dim gzip As New GZipArchive(gzipFile)
    
            For Each file As AbstractFile In gzip.GetFiles(True)
                Console.WriteLine(file.FullName)
            Next file
        End If
    End Sub

    This example demonstrates how to create a multi-file GZip archive.

    C#
    Copy Code
    public static void WriteExample()
    {
      // This static property must be set before the GZipArchive is created.
      GZipArchive.AllowMultipleFiles = true;
    
      // Select a GZip file
      AbstractFile gzipFile = new DiskFile( @"D:\test.gz" );
    
      // If the physical GZip file already exists
      if( gzipFile.Exists )
      {
        // Delete it. We want to create a new archive, not update an existing one
        gzipFile.Delete();
      }
    
      // Wrap the physical GZip around a logical GZipArchive object
      GZipArchive gzip = new GZipArchive( gzipFile );
    
      // Select a source folder
      AbstractFolder sourceFolder = new DiskFolder( @"D:\Data" );
    
      // Batch the following operations into the Tar archive
      using( AutoBatchUpdate batch = new AutoBatchUpdate( gzip ) )
      {
        // The 'recursive' parameter must false when using the CopyFilesTo method with a GZip archive; otherwise, an exception will be thrown if subfolders are present in the source
        sourceFolder.CopyFilesTo( gzip, false, true );
      }
    }
    VB.NET
    Copy Code
    Public Shared Sub WriteExample()
        ' This static property must be set before the GZipArchive is created.
        GZipArchive.AllowMultipleFiles = True
    
        ' Select a GZip file
        Dim gzipFile As AbstractFile = New DiskFile("D:\test.gz")
    
        ' If the physical GZip file already exists
        If gzipFile.Exists Then
            ' Delete it. We want to create a new archive, not update an existing one
            gzipFile.Delete()
        End If
    
        ' Wrap the physical GZip around a logical GZipArchive object
        Dim gzip As New GZipArchive(gzipFile)
    
        ' 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(gzip)
            ' The 'recursive' parameter must false when using the CopyFilesTo method with a GZip archive; otherwise, an exception will be thrown if subfolders are present in the source
            sourceFolder.CopyFilesTo(gzip, False, True)
        End Using
    End Sub

    Things you should consider

    The main questions you should ask yourself when performing these operations with a GZip archive are: