ZipInputStream Constructor (stream)

DotNetZip

Ionic Zip Library v1.9.1.6 ZipInputStream Constructor (stream)
ReferenceIonic.ZipZipInputStreamZipInputStream(Stream)
Create a ZipInputStream, wrapping it around an existing stream.
Declaration Syntax
C# Visual Basic Visual C++
public ZipInputStream(
	Stream stream
)
Public Sub New ( _
	stream As Stream _
)
public:
ZipInputStream(
	Stream^ stream
)
Parameters
stream (Stream)
The stream to read. It must be readable. This stream will be closed at the time the ZipInputStream is closed.
Remarks

While the ZipFile class is generally easier to use, this class provides an alternative to those applications that want to read from a zipfile directly, using a Stream.

Both the ZipInputStream class and the ZipFile class can be used to read and extract zip files. Both of them support many of the common zip features, including Unicode, different compression levels, and ZIP64. The programming models differ. For example, when extracting entries via calls to the GetNextEntry() and Read() methods on the ZipInputStream class, the caller is responsible for creating the file, writing the bytes into the file, setting the attributes on the file, and setting the created, last modified, and last accessed timestamps on the file. All of these things are done automatically by a call to ZipEntry.Extract(). For this reason, the ZipInputStream is generally recommended for when your application wants to extract the data, without storing that data into a file.

Aside from the obvious differences in programming model, there are some differences in capability between the ZipFile class and the ZipInputStream class.

  • ZipFile can be used to create or update zip files, or read and extract zip files. ZipInputStream can be used only to read and extract zip files. If you want to use a stream to create zip files, check out the ZipOutputStream.
  • ZipInputStream cannot read segmented or spanned zip files.
  • ZipInputStream will not read Zip file comments.
  • When reading larger files, ZipInputStream will always underperform ZipFile. This is because the ZipInputStream does a full scan on the zip file, while the ZipFile class reads the central directory of the zip file.
Examples
This example shows how to read a zip file, and extract entries, using the ZipInputStream class.
CopyC#
private void Unzip()
{
    byte[] buffer= new byte[2048];
    int n;
    using (var raw = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
    {
        using (var input= new ZipInputStream(raw))
        {
            ZipEntry e;
            while (( e = input.GetNextEntry()) != null)
            {
                if (e.IsDirectory) continue;
                string outputPath = Path.Combine(extractDir, e.FileName);
                using (var output = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite))
                {
                    while ((n= input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        output.Write(buffer,0,n);
                    }
                }
            }
        }
    }
}
CopyVB.NET
Private Sub UnZip()
    Dim inputFileName As String = "MyArchive.zip"
    Dim extractDir As String = "extract"
    Dim buffer As Byte() = New Byte(2048) {}
    Using raw As FileStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read)
        Using input As ZipInputStream = New ZipInputStream(raw)
            Dim e As ZipEntry
            Do While (Not e = input.GetNextEntry Is Nothing)
                If Not e.IsDirectory Then
                    Using output As FileStream = File.Open(Path.Combine(extractDir, e.FileName), _
                                                           FileMode.Create, FileAccess.ReadWrite)
                        Dim n As Integer
                        Do While (n = input.Read(buffer, 0, buffer.Length) > 0)
                            output.Write(buffer, 0, n)
                        Loop
                    End Using
                End If
            Loop
        End Using
    End Using
End Sub

Assembly: Ionic.Zip (Module: Ionic.Zip) Version: 1.9.1.8 (1.9.1.8)