Xceed .NET Libraries Documentation
Optimizing batch updates to a folder

Welcome to Xceed .NET, .NET Standard and Xamarin Libraries! > Basic Concepts > Zip and streaming capabilities > Optimizing batch updates to a folder

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:

VB.NET Copy Code

Imports Xceed.FileSystem
Imports Xceed.Zip 

Dim archive As New ZipArchive ( New DiskFile( "c:\test.zip" ) ) 

archive.BeginUpdate()

    'Code

archive.EndUpdate()

C# Copy Code

using Xceed.FileSystem;
using Xceed.Zip; 

ZipArchive archive = new ZipArchive ( new DiskFile( @"c:\test.zip" ) ); 

archive.BeginUpdate();

    //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:

  • Verify if the folder class implements the IBatchUpdateable interface. This can only be done via the root of the folder. 

  • If the IBatchUpdateable interface is implemented, call the BeginUpdate method to mark the begin of your scope. 

  • Do the various operations on the folder. 

  • To commit the modifications made to the folder, call the EndUpdate method. It is important to make sure that the EndUpdate method is always called otherwise the modifications will not be committed.

Demonstration

This example demonstrates how to use the IBatchUpdateable interface directly:

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

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
{
  //Perform the various operations.
}
finally
{
  if( batch != null )
    batch.EndUpdate();

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

VB.NET Copy Code

'folder represents any type of folder
Dim batch As New AutoBatchUpdate( folder.RootFolder ) 

'Perform the various operations.
'Commit the modifications to the folder

batch.Dispose()

C# Copy Code

//folder represents any type of folder
using( AutoBatchUpdate batch = new AutoBatchUpdate( folder.RootFolder ) )

  //Perform the various operations.
  //Once the scope of the using statement is exited, if the folder class implemented
  //the IBatchUpdateable interface, the changes to the folder will be committed.
}