Forward-Only ZIP Extraction from Streams

Overview

ZIP archives are usually opened from a seekable file or stream because the central directory is stored near the end of the archive. In server applications, however, the archive often arrives as a non-seekable stream such as an HTTP request body, NetworkStream, or pipe. In those cases Aspose.ZIP for .NET can extract the archive sequentially with ArchiveLoadOptions.ForwardOnly.

This mode is useful when you want to start unpacking data immediately and avoid buffering the whole ZIP file to a temporary seekable storage first.

Forward-only ZIP extraction is sequential. Use it when the source stream cannot seek and the goal is to unpack the archive as it is being read. Random access through archive.Entries, archive modification, and resaving are not available in this mode. If you need to inspect or extract arbitrary entries by index or name, copy the archive to a seekable stream or file and open it without ForwardOnly.

Extract a ZIP Archive from a Non-Seekable Stream

The following example shows the standard pattern. Here GetNonSeekableZipStream() represents any application-specific source that does not support seeking.

1    using (Stream sourceStream = GetNonSeekableZipStream())
2    using (Archive archive = new Archive(sourceStream, new ArchiveLoadOptions()
3    {
4        ForwardOnly = true
5    }))
6    {
7        archive.ExtractToDirectory("extracted");
8    }

Extract an Encrypted ZIP from a Stream

Password-protected ZIP archives can also be extracted in forward-only mode. Supply the password in ArchiveLoadOptions.

1    using (Stream sourceStream = GetNonSeekableZipStream())
2    using (Archive archive = new Archive(sourceStream, new ArchiveLoadOptions()
3    {
4        ForwardOnly = true,
5        DecryptionPassword = "p@s$"
6    }))
7    {
8        archive.ExtractToDirectory("extracted");
9    }

Cancel Forward-Only Extraction

Stream-driven extraction is commonly used in services and background jobs, where time limits matter. You can combine ForwardOnly with CancellationToken to stop long-running extraction.

 1    using (CancellationTokenSource cts = new CancellationTokenSource())
 2    {
 3        cts.CancelAfter(TimeSpan.FromSeconds(20));
 4
 5        using (Stream sourceStream = GetNonSeekableZipStream())
 6        using (Archive archive = new Archive(sourceStream, new ArchiveLoadOptions()
 7        {
 8            ForwardOnly = true,
 9            CancellationToken = cts.Token
10        }))
11        {
12            archive.ExtractToDirectory("extracted");
13        }
14    }

When to Use Forward-Only Mode

Use ForwardOnly when the archive source is sequential by nature, for example:

If the ZIP file is already stored on disk or loaded into a seekable MemoryStream, the regular Archive constructor is usually the better choice because it allows entry inspection and random access extraction.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.