Rebex ZIP

ZIP and GZIP compression .NET library

Download 30-day free trial Buy from $199
More .NET libraries

Back to feature list...

ZipReader class

ZipReader basics 

ZipReader class makes it possible to process ZIP archives sequentially. This is useful when working with non-seekable streams, such as ZIP files streamed from the Internet, and iterate through ZIP items or extract them on-the-fly.

Additionally, ZipReader can process incomplete ZIP archives (for example due to interrupted download) that lack the central directory structure, located at the end of a ZIP archive).

ZipReader is more efficient than our easy-to-use API (the ZipArchive class), because it does not have to parse the central directory in advance and keep it in memory. ZipReader is not as simple to use - but it targets scenarios that ZipArchive cannot handle.

To iterate through a ZIP file using ZipReader, use ReadNext() method:

// initialize ZipReader from a (possibly non-seekable) stream
using (var reader = new ZipReader(stream))
{
    // iterate through all items in the ZIP archive
    while (reader.ReadNext())
    {
        // do something...
    }
}

ZipReader properties 

Use ZipReader.ReadNext() method to iterate through the ZIP archive sequentially. When it returns true, it indiates that it has advanced to a new ZIP item, and properties on ZipReader class are populated to provide information about it.

For example, use this code to list ZIP archive contents:

// initialize ZipReader from a (possibly non-seekable) stream
using (var reader = new ZipReader(stream))
{
    // iterate through the ZIP archive and list all items
    while (reader.ReadNext())
    {
        // get item type
        string type = "?";
        switch (reader.ItemType)
        {
            case ArchiveItemType.File: type = "-"; break;
            case ArchiveItemType.Directory: type = "d"; break;
            case ArchiveItemType.Link: type = "l"; break;
        }

        // get last modification date
        string modified;
        if (reader.LastWriteTime.HasValue)
        {
            modified = reader.LastWriteTime.Value.ToString("yyyy-MM-dd");
        }
        else
        {
            modified = "????-??-??";
        }

        // print info about currently processing ZIP item
        Console.WriteLine("{0}\t{1}\t{2}\t{3}",
            type, modified, reader.Length, reader.Path);
    }
}

Extracting using ZipReader 

To extract data from a ZIP archive using ZipReader class, iterate through its items and use ZipReader.Extract() method to uncompress them:

// open an incomplete ZIP archive
using (var reader = new ZipReader(@"C:\MyData\incomplete-archive.zip"))
{
    // specify output directory
    string outputPath = @"C:\MyData\Out";

    // iterate through all items
    while (reader.ReadNext())
    {
        // skip directories
        if (reader.IsDirectory)
            continue;

        if (reader.CanExtract)
        {
            try
            {
                // try to extract the item
                reader.Extract(Path.Combine(outputPath, reader.Path));
                Console.WriteLine("Extracted '{0}'.", reader.Path);
            }
            catch (ZipException ex)
            {
                // handle errors (such as incomplete last item)
                Console.WriteLine("Cannot extract item '{0}': {1}",
                    reader.Path, ex.Message);
            }
        }
        else
        {
            // report error for unsupported items
            Console.WriteLine("Cannot extract item '{0}'.", reader.Path);
        }
    }
}

Back to feature list...