Welcome to Xceed Real-Time Zip for .NET, .NET Standard & Xamarin > Task-Based Help > Using the ByteProgression and InvalidPassword events (ZipReader) |
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