Xceed Real-Time Zip for .NET Documentation
Events

Welcome to Xceed Real-Time Zip for .NET, .NET Standard & Xamarin > Basic Concepts > Events

The ZipWriter and ZipReader classes provides events that let you monitor the progress of the Zip archive creation and extraction processes. Three types of events are available, which are described and demonstrated below.

Event Description
ByteProgression (writing, reading) Used to access byte progression information when reading or writing a Zip archive. Available in ZipWriter and ZipReader.
InvalidPassword Used when an invalid password is provided.

The ByteProgression event

The ZipWriter and ZipReader classes both expose a ByteProgression event, which is raised when WriteItemData or ReadItemData is called.

A ZipWriterByteProgressionEventArgs object provides the following properties for use in ZipWriter's ByteProgression event handler:

Property Description
BytesProcessed The number of bytes processed of the current file.
ZipItemLocalHeader The local header of the item currently being processed.

A ZipReaderByteProgressionEventArgs object provides the following properties for use in ZipReader's ByteProgression event handler:

Property Description
BytesProcessed The number of bytes processed of the current file.
Percent The percentage of the current file that has been processed.
UncompressedSize The uncompressed size of the current file.
ZipItemLocalHeader The local header of the file currently being processed.
When the compressed/uncompressed sizes are unknown, UncompressedSize returns -1 and Percent returns 0, and must therefore not be used. This occurs for example with Zip archives created using ZipWriter, because a backwards seek before the compressed data of a file cannot be performed. Consequently, the values for compressed/uncompressed sizes cannot be modified. A Zip application such as WinZip, however, can seek back and write these values. As a result, when reading such an archive, UncompressedSize and Percent return useful values.

The InvalidPassword event

The ZipReader class additionally exposes an InvalidPassword event, which is raised when no password or an invalid password is provided to a password-protected archive. A ZipReaderInvalidPasswordEventArgs object provides the following properties for use in ZipReader's  InvalidPassword event handler:

Property Description
Abort Indicates whether the Zip read operation should be aborted.
NewPassword The new password to be used during the Zip read operation.
OldPassword The password initially supplied.
ZipItemLocalHeader The local header of the file currently being processed.

Examples

Example 1: Using the ByteProgression event (ZipWriter)

The following examples demonstrate how to use ZipWriter's ByteProgression event, as well as its event arguments.
using System.IO;

using Xceed.Zip.ReaderWriter;

//The target Zip archive.

using (FileStream fileStream1 = new FileStream(@"d:\testOutput\test.zip",

  FileMode.Create, FileAccess.Write))

{

  //Create a ZipWriter object around the stream.

  Xceed.Zip.ReaderWriter.ZipWriter zipWriter1 =

    new Xceed.Zip.ReaderWriter.ZipWriter(fileStream1);

  //Subscribe to the ByteProgression event

  zipWriter1.ByteProgression += new

    EventHandler<ZipWriterByteProgressionEventArgs>(zipWriter1_ByteProgression);

  DirectoryInfo directoryInfo = new DirectoryInfo(@"d:\test\");

  if (directoryInfo.Exists)

  {

    FileInfo[] files = directoryInfo.GetFiles("*.*",

      SearchOption.AllDirectories);

    foreach (FileInfo file in files)

    {

      //Create ZipItemLocalHeader for current item and write to archive.

      ZipItemLocalHeader zipItemLocalHeader1 = new ZipItemLocalHeader

        (file.Name);

      zipWriter1.WriteItemLocalHeader(zipItemLocalHeader1);

      byte[] buffer = new byte[1024];

      int read = 0;

      using (FileStream fs = file.OpenRead())

      {

        //Read the current item's data

        while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)

        {

          //Write the current item's data to the Zip archive

          zipWriter1.WriteItemData(buffer, 0, read);

        }

      }

    }

    //Close the Zip archive

    zipWriter1.CloseZipFile();

    Console.WriteLine("Zip archive created.");

  }

}



//The ByteProgression event's handler. Demonstrates the use of

//the properties of ZipWriterByteProgressionEventArgs.

static void zipWriter1_ByteProgression(object sender, 

  ZipWriterByteProgressionEventArgs e)

{

  Console.WriteLine("Item {0}: CompressionMethod = {1},

    CompressionLevel = {2}.", e.ZipItemLocalHeader.FileName,

    e.ZipItemLocalHeader.CompressionMethod,

    e.ZipItemLocalHeader.CompressionLevel);

}
Imports System.IO

Imports Xceed.Zip.ReaderWriter

'The target Zip archive.

Using fileStream1 As New FileStream("d:\testOutput\test.zip",

  FileMode.Create, FileAccess.Write)



  'Create a ZipWriter object around the stream.

  Dim zipWriter1 As New Xceed.Zip.ReaderWriter.ZipWriter(fileStream1)

  'Subscribe to the ByteProgression event.

  AddHandler zipWriter1.ByteProgression, AddressOf Of

    ZipWriterByteProgressionEventArgs



  Dim directoryInfo As New DirectoryInfo("d:\test\")

  If directoryInfo.Exists Then

    Dim files As FileInfo() = directoryInfo.GetFiles("*.*",

      SearchOption.AllDirectories)

    For Each file As FileInfo In files

      'Create ZipItemLocalHeader for current item and write to archive.

      Dim zipItemLocalHeader1 As New ZipItemLocalHeader(file.Name)

      zipWriter1.WriteItemLocalHeader(zipItemLocalHeader1)

      Dim buffer As Byte() = New Byte(1023){}

      Dim read As Integer = 0

      Using fs As FileStream = file.OpenRead()

        'Read the current item's data

        read = fs.Read(buffer, 0, buffer.Length)

        Do While (read <> 0)

          'Write the current item's data to the Zip archive

          zipWriter1.WriteItemData(buffer, 0, read)

          read = fs.Read(buffer, 0, buffer.Length)

        Loop

      End Using

    Next file

    'Close the Zip archive

    zipWriter1.CloseZipFile()

    Console.WriteLine("Zip archive created.")

  End If

End Using



'The ByteProgression event's handler. Demonstrates the use of

'the properties of ZipWriterByteProgressionEventArgs.

Shared Sub zipWriter1_ByteProgression(ByVal sender As Object, ByVal e As ZipWriterByteProgressionEventArgs)

  Console.WriteLine("Item {0}: CompressionMethod = {1},

    CompressionLevel = {2}.", e.ZipItemLocalHeader.FileName,

    e.ZipItemLocalHeader.CompressionMethod,

    e.ZipItemLocalHeader.CompressionLevel)

End Sub

Example 2: Using the ByteProgression and InvalidPassword events (ZipReader)

The following examples demonstrate how to use ZipReader's ByteProgression and InvalidPassword events, as well as their event arguments.
using System.IO;

using Xceed.Zip.ReaderWriter;

//The source Zip archive.

using (FileStream fileStream1 = new FileStream(@"d:\testOutput\test.zip",

  FileMode.Open, FileAccess.Read))

{

  //Must seek to the beginning of the stream before reading.

  fileStream1.Seek(0, SeekOrigin.Begin);

  //Create a ZipReader around the stream.

  Xceed.Zip.ReaderWriter.ZipReader zipReader1 =

    new Xceed.Zip.ReaderWriter.ZipReader(fileStream1);

  //Subscribe to the ByteProgression event

  zipReader1.ByteProgression +=

    new EventHandler<ZipReaderByteProgressionEventArgs>

    (zipReader1_ByteProgression);

  //Subscribe to the InvalidPassword event

  zipReader1.InvalidPassword +=

    new EventHandler<ZipReaderInvalidPasswordEventArgs>

    (zipReader1_InvalidPassword);

  ZipItemLocalHeader zipItemLocalHeader = null;

  //Read the local headers until no more are found

  while ((zipItemLocalHeader = zipReader1.ReadItemLocalHeader()) != null)

  {

    byte[] buffer = new byte[1024];

    int read = 0;

    //Read the item's data

    while ((read = zipReader1.ReadItemData(buffer, 0, buffer.Length)) > 0)

    {

      //Do something with the data in 'buffer'

    }

  }

}



//The InvalidPassword event's handler. Demonstrates the use of

//the properties of ZipReaderInvalidPasswordEventArgs.

static void zipReader1_InvalidPassword(object sender,

  ZipReaderInvalidPasswordEventArgs e)

{

  Console.Write("Enter password for file {0} (<Enter> alone to abort): ",

    e.ZipItemLocalHeader.FileName);

  string password = Console.ReadLine();

  if (string.IsNullOrEmpty(password))

  {

    //Set Abort to true to abort the read operation.

    e.Abort = true;

  }

  else

  {

    //Set NewPassword to the provided password. If it is the correct password,

    //the read operation will proceed. Otherwise, the InvalidPassword event is

    //raised again.

    e.NewPassword = password;

  }

}

//The ByteProgression event's handler. Demonstrates the use of

//the properties of ZipReaderByteProgressionEventArgs.

static void zipReader1_ByteProgression(object sender,

   ZipReaderByteProgressionEventArgs e)

{

  if (e.UncompressedSize == -1)

    Console.WriteLine("Processing item {0}: {1} bytes processed.",

      e.ZipItemLocalHeader.FileName, e.BytesProcessed);

  else

  {

    //UncompressedSize is not -1, so this property and the Percent property

    //return useful values.

    Console.WriteLine("Processing item {0}: {1} bytes processed ({3}%).

      (Uncompressed size = {3}.)",

      e.ZipItemLocalHeader.FileName,

      e.BytesProcessed,

      e.UncompressedSize,

      e.Percent);

  }

}
Imports System.IO

Imports Xceed.Zip.ReaderWriter

Using fileStream1 As New FileStream("d:\testOutput\test.zip",

  FileMode.Open, FileAccess.Read)



  'Must seek to the beginning of the stream before reading.

  fileStream1.Seek(0, SeekOrigin.Begin)

  'Create a ZipReader around the stream.

  Dim zipReader1 As New Xceed.Zip.ReaderWriter.ZipReader(fileStream1)

  'Subscribe to the ByteProgression event

  AddHandler zipReader1.ByteProgression, AddressOf Of

    ZipReaderByteProgressionEventArgs

  'Subscribe to the InvalidPassword event

  AddHandler zipReader1.InvalidPassword, AddressOf Of

    ZipReaderInvalidPasswordEventArgs

  Dim zipItemLocalHeader As ZipItemLocalHeader = Nothing

  'Read the local headers until no more are found

  Do While Not (zipItemLocalHeader = zipReader1.ReadItemLocalHeader()) Is

  Nothing

    Dim buffer As Byte() = New Byte(1023){}

    Dim read As Integer = 0

    'Read the item's data

    Do While (read = zipReader1.ReadItemData(buffer, 0, buffer.Length)) > 0

      'Do something with the data in 'buffer'

    Loop

  Loop

End Using



'The InvalidPassword event's handler. Demonstrates the use of

'the properties of ZipReaderInvalidPasswordEventArgs.

Shared Sub zipReader1_InvalidPassword(ByVal sender As Object, ByVal e As

  ZipReaderInvalidPasswordEventArgs)

  Console.Write("Enter password for file {0} (<Enter> alone to abort): ",

    e.ZipItemLocalHeader.FileName)

  Dim password As String = Console.ReadLine()

  'Set Abort to true to abort the read operation.

  If String.IsNullOrEmpty(password) Then

    e.Abort = True

  Else

    'Set NewPassword to the provided password. If it is the correct password,

    'the read operation will proceed. Otherwise, the InvalidPassword event is

    'raised again.

    e.NewPassword = password

  End If

End Sub



'The ByteProgression event's handler. Demonstrates the use of

'the properties of ZipReaderByteProgressionEventArgs.

Shared Sub zipReader1_ByteProgression(ByVal sender As Object, ByVal e As

  ZipReaderByteProgressionEventArgs)

  If e.UncompressedSize = -1 Then

    Console.WriteLine("Processing item {0}: {1} bytes processed.",

      e.ZipItemLocalHeader.FileName, e.BytesProcessed)

  Else

    'UncompressedSize is not -1, so this property and the Percent

    'property return useful values.

    Console.WriteLine("Processing item {0}: {1} bytes processed

      ({3}%). (Uncompressed size = {3}.)",

      e.ZipItemLocalHeader.FileName,

      e.BytesProcessed, e.UncompressedSize, e.Percent)

  End If

End Sub