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 = newbyte[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.
staticvoid 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.
staticvoid 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 AsNew 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 AsNew Xceed.Zip.ReaderWriter.ZipReader(fileStream1)
'Subscribe to the ByteProgression event
AddHandler zipReader1.ByteProgression, AddressOfOf
ZipReaderByteProgressionEventArgs
'Subscribe to the InvalidPassword event
AddHandler zipReader1.InvalidPassword, AddressOfOf
ZipReaderInvalidPasswordEventArgs
Dim zipItemLocalHeader As ZipItemLocalHeader = Nothing'Read the local headers until no more are found
DoWhileNot (zipItemLocalHeader = zipReader1.ReadItemLocalHeader()) IsNothingDim buffer AsByte() = NewByte(1023){}
Dim read AsInteger = 0
'Read the item's data
DoWhile (read = zipReader1.ReadItemData(buffer, 0, buffer.Length)) > 0
'Do something with the data in 'buffer'
LoopLoopEndUsing'The InvalidPassword event's handler. Demonstrates the use of
'the properties of ZipReaderInvalidPasswordEventArgs.
SharedSub zipReader1_InvalidPassword(ByVal sender AsObject, ByVal e As
ZipReaderInvalidPasswordEventArgs)
Console.Write("Enter password for file {0} (<Enter> alone to abort): ",
e.ZipItemLocalHeader.FileName)
Dim password AsString = Console.ReadLine()
'Set Abort to true to abort the read operation.
IfString.IsNullOrEmpty(password) Then
e.Abort = TrueElse'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
EndIfEnd Sub'The ByteProgression event's handler. Demonstrates the use of
'the properties of ZipReaderByteProgressionEventArgs.
SharedSub zipReader1_ByteProgression(ByVal sender AsObject, 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)
EndIfEnd Sub