Xceed .NET Libraries Documentation
Extracting data from a Zip archive using ZipReader Snippet

The following example shows how to read a Zip archive.

using System;
using System.IO;
using Xceed.Zip.ReaderWriter;
namespace RealTimeZipExamples
{
  class Examples
  {
    public static void TheZipReaderClass()
    {
      // Pick a zip file
      string zipFileName = @"D:\SomeFolder\SomeZipFile.zip";
      // Pick a base output folder, taking care not to end it with a directory separator
      string unzipToFolder = @"D:\SomeOtherFolder";
      // Open the source Zip file
      using( FileStream zipFileStream = new FileStream( zipFileName, FileMode.Open, FileAccess.Read ) )
      {
        // Create a Zip reader object around the stream.
        // Remember that ZipReader doesn't close the underlying stream given here
        using( ZipReader zipReader = new ZipReader( zipFileStream ) )
        {
          // Optional. Provide the default password for encrypted items in the archive
          zipReader.EncryptionPassword = "Password";
          // Optional. Subscribe to available events
          zipReader.ByteProgression += new EventHandler<ZipReaderByteProgressionEventArgs>( OnByteProgression );
          zipReader.InvalidPassword += new EventHandler<ZipReaderInvalidPasswordEventArgs>( OnInvalidPassword );
          ZipItemLocalHeader zipItemLocalHeader;
          // While the reader finds local headers
          while( ( zipItemLocalHeader = zipReader.ReadItemLocalHeader() ) != null )
          {
            // The 'FileName' property contains the sub-folders and filename
            string outputPath = unzipToFolder + zipItemLocalHeader.FileName;
            string outputFolder = Path.GetDirectoryName( outputPath );
            // Make sure the output folder exists
            Directory.CreateDirectory( outputFolder );
            // If the item isn't a folder entry
            if( !zipItemLocalHeader.IsFolder )
            {
              // Create/overwrite an output file using our calculated filename
              using( FileStream outputFileStream = new FileStream( outputPath, FileMode.Create, FileAccess.Write ) )
              {
                // Have the reader read the item data and write it to the stream using a 64K buffer
                zipReader.ReadItemData( outputFileStream, 64 * 1024 );
              }
            }
          }
          // Optional. Have the reader give us the zip ending header
          ZipEndHeader endHeader = zipReader.ReadEndHeader();
          // If the header contains a global zip comment
          if( endHeader != null && !String.IsNullOrEmpty( endHeader.ZipComment ) )
          {
            // TODO: Do something with the global zip comment
          }
        }
      }
    }
    private static void OnInvalidPassword( object sender, ZipReaderInvalidPasswordEventArgs e )
    {
      // TODO: We have access to the current item being unzipped. We can report it's name, etc
      ZipItemLocalHeader currentItem = e.ZipItemLocalHeader;
      // TODO: We're given the password that failed. We can report it, etc
      string oldPassword = e.OldPassword;
      /* If 'e.NewPassword' is set to null or an empty string, or 'e.Abort' is set to true,
      a ZipReaderException will be thrown for failure to decrypt the item.
      Since items can't be skipped, the entire unzip process will be canceled.
       
      When the event is triggered, 'e.NewPassword' is set to an empty string and 'e.Abort' is set
      to false. */
      // TODO: We have to supply a new password.
      // If that new password is invalid, the event will be triggered again
      e.NewPassword = "Some New Password";
      // TODO: We can ask the entire unzip operation to be aborted
      e.Abort = true;
    }
    private static void OnByteProgression( object sender, ZipReaderByteProgressionEventArgs e )
    {
      // TODO: We have access to the current item being unzipped. We can report it's name, etc
      ZipItemLocalHeader currentItem = e.ZipItemLocalHeader;
      // TODO: We're given the current amount of bytes unzipped for the item. We can report it, etc
      long bytesProcessed = e.BytesProcessed;
      /* Do not assume that e.UncompressedSize and e.Percent will contain useful values.
      Since ZipReader doesn't seek in the archive, it cannot always know the uncompressed
      size in advance. */
    }
  }
}
Imports System
Imports System.IO
Imports Xceed.Zip.ReaderWriter
Namespace RealTimeZipExamples
  Friend Class Examples
    Public Shared Sub TheZipReaderClass()
      ' Pick a zip file
      Dim zipFileName As String = "D:\SomeFolder\SomeZipFile.zip"
      ' Pick a base output folder, taking care not to end it with a directory separator
      Dim unzipToFolder As String = "D:\SomeOtherFolder"
      ' Open the source Zip file
      Using zipFileStream As New FileStream(zipFileName, FileMode.Open, FileAccess.Read)
        ' Create a Zip reader object around the stream.
        ' Remember that ZipReader doesn't close the underlying stream given here
        Using zipReader As New ZipReader(zipFileStream)
          ' Optional. Provide the default password for encrypted items in the archive
          zipReader.EncryptionPassword = "Password"
          ' Optional. Subscribe to available events
          AddHandler zipReader.ByteProgression, AddressOf OnByteProgression
          AddHandler zipReader.InvalidPassword, AddressOf OnInvalidPassword
          Dim zipItemLocalHeader As ZipItemLocalHeader
          ' While the reader finds local headers
          zipItemLocalHeader = zipReader.ReadItemLocalHeader()
          Do While zipItemLocalHeader IsNot Nothing
            ' The 'FileName' property contains the sub-folders and filename
            Dim outputPath As String = unzipToFolder & zipItemLocalHeader.FileName
            Dim outputFolder As String = Path.GetDirectoryName(outputPath)
            ' Make sure the output folder exists
            Directory.CreateDirectory(outputFolder)
            ' If the item isn't a folder entry
            If (Not zipItemLocalHeader.IsFolder) Then
              ' Create/overwrite an output file using our calculated filename
              Using outputFileStream As New FileStream(outputPath, FileMode.Create, FileAccess.Write)
                ' Have the reader read the item data and write it to the stream using a 64K buffer
                zipReader.ReadItemData(outputFileStream, 64 * 1024)
              End Using
            End If
              zipItemLocalHeader = zipReader.ReadItemLocalHeader()
          Loop
          ' Optional. Have the reader give us the zip ending header
          Dim endHeader As ZipEndHeader = zipReader.ReadEndHeader()
          ' If the header contains a global zip comment
          If endHeader IsNot Nothing AndAlso (Not String.IsNullOrEmpty(endHeader.ZipComment)) Then
            ' TODO: Do something with the global zip comment
          End If
        End Using
      End Using
    End Sub
    Private Shared Sub OnInvalidPassword(ByVal sender As Object, ByVal e As ZipReaderInvalidPasswordEventArgs)
      ' TODO: We have access to the current item being unzipped. We can report it's name, etc
      Dim currentItem As ZipItemLocalHeader = e.ZipItemLocalHeader
      ' TODO: We're given the password that failed. We can report it, etc
      Dim oldPassword As String = e.OldPassword
'      If 'e.NewPassword' is set to null or an empty string, or 'e.Abort' is set to true,
'      a ZipReaderException will be thrown for failure to decrypt the item.
'      Since items can't be skipped, the entire unzip process will be canceled.
'       
'      When the event is triggered, 'e.NewPassword' is set to an empty string and 'e.Abort' is set
'      to false.
      ' TODO: We have to supply a new password.
      ' If that new password is invalid, the event will be triggered again
      e.NewPassword = "Some New Password"
      ' TODO: We can ask the entire unzip operation to be aborted
      e.Abort = True
    End Sub
    Private Shared Sub OnByteProgression(ByVal sender As Object, ByVal e As ZipReaderByteProgressionEventArgs)
      ' TODO: We have access to the current item being unzipped. We can report it's name, etc
      Dim currentItem As ZipItemLocalHeader = e.ZipItemLocalHeader
      ' TODO: We're given the current amount of bytes unzipped for the item. We can report it, etc
      Dim bytesProcessed As Long = e.BytesProcessed
'      Do not assume that e.UncompressedSize and e.Percent will contain useful values.
'      Since ZipReader doesn't seek in the archive, it cannot always know the uncompressed
'      size in advance.
    End Sub
  End Class
End Namespace