[Root] / Xceed Data Manipulation Components for .NET / [Root] / Basic Concepts / [Root] / Zip and streaming capabilities / Optimizing batch updates to a folder

In This Topic
    Optimizing batch updates to a folder
    In This Topic

    When performing multiple operations on a folder, the process can take a while to complete. Although events can provide feedback during these lengthy operations, it is also possible to reduce the time it takes to accomplish all the operations if the folder class being used implements the IBatchUpdateable interface.

    The IBatchUpdateable interface is defined within the Xceed.FileSystem namespace and is implemented by the "Archive" classes, namely, the ZipArchive, TarArchive and GZipArchive classes.

    IBatchUpdateable interface

    The IBatchUpdateable interface implements the BeginUpdate and EndUpdate methods which allow you to determine a scope at the end of which all the modifications made to a folder will be committed. Using this interface increases the speed at which the operations on the folder are accomplished since the modifications are not committed to the folder each time a method is called but rather only once for all the operations when the EndUpdate method is called.

    If a class implements the IBatchUpdateable interface, the BeginUpdate and EndUpdate methods can be called directly on the class. The following example shows how to do this with a Zip archive:

    C#
    Copy Code
    using Xceed.FileSystem;
    using Xceed.Zip; 
    
    ZipArchive archive = new ZipArchive ( new DiskFile( @"c:\test.zip" ) );
    
    archive.BeginUpdate();
    
    // TODO: Code
    
    archive.EndUpdate();
    VB.NET
    Copy Code
    Imports Xceed.FileSystem
    Imports Xceed.Zip 
    
    Dim archive As New ZipArchive ( New DiskFile( "c:\test.zip" ) ) 
    
    archive.BeginUpdate()
    
    ' TODO: Code
    
    archive.EndUpdate()

    Classes that implement the IBatchUpdateable interface

    The AutoBatchUpdate class will verify to see that the folder class passed in its constructor implements the IBatchUpdateable interface and will automatically call the BeginUpdate and EndUpdate methods if it does. If the folder class does not implement the IBatchUpdateable interface, the code will be executed normally.

    Basic steps

    To perform batch updates, the following steps must be performed:

    Demonstration

    This example demonstrates how to use the IBatchUpdateable interface directly:

    C#
    Copy Code
    using Xceed.FileSystem
     
    // 'folder' represents any type of folder
    IBatchUpdateable batch = folder.RootFolder as IBatchUpdateable;
              
    // Check if our folder class implements the IBatchUpdateable interface via it's root
    // If it does, call the BeginUpdate method.
    if( batch != null )
    {
      batch.BeginUpdate();
    }
    
    try
    {
      // TODO: Perform the various operations.
    }
    finally
    {
      if( batch != null )
      {
        batch.EndUpdate();
      }
    }
    VB.NET
    Copy Code
    Imports Xceed.FileSystem
    
    Dim batch As IBatchUpdateable = Nothing
    
    If TypeOf folder.RootFolder Is IBatchUpdateable Then
      batch = folder.RootFolder
      batch.BeginUpdate()
    End If
    
    Try
    'Perform the various operations
    Finally
      If Not batch Is Nothing Then
        batch.EndUpdate()
      End If
    End Try

    This example demonstrates how to use the AutoBatchUpdate class rather than manually calling the BeginUpdate and EndUpdate methods manually:

    C#
    Copy Code
    ZipArchive zip = new ZipArchive( new DiskFile( @"c:\test.zip" ) );
    
    using( AutoBatchUpdate batch = new AutoBatchUpdate( zip ) )
    { 
      // TODO: Perform various operations on zip and any of its sub folders.
    
      /* Once the using block goes out of scope, the changes to the folder will be committed. */
    }
    VB.NET
    Copy Code
    Dim zip As New ZipArchive(New DiskFile("c:\test.zip"))
    
    Using batch As New AutoBatchUpdate(zip)
      ' TODO: Perform various operations on zip and any of its sub folders.
    
      ' Once the using block goes out of scope, the changes to the folder will be committed. 
    End Using