The method creates a new read-only, non-seeking stream object based on the ZipReader instance's current item. Each call to the stream's Stream.Read method will call ReadItemData.
The method allows you to use the Stream class interface to read item data from ZipReader instead of using ReadItemData() directly. This makes it possible to integrate ZipReader with other classes that use the Stream class interface without the need for "glue code."
Dispose should be called on the stream when all the current item's data has been read.
The stream returned by the method should be considered unique to the current item. This means you should not reuse the stream with later items. Instead, get a new stream instance by calling the method again. The example below illustrates this.
ReadItemData can still be called to read data even if this method is used. This can be done after the stream returned by the method has been closed or even while the stream is active.
The method can be used to read nested zip archives with ZipReader. A nested zip archive is when an item in an archive is another zip archive. By providing the stream returned by the method to a new instance of ZipReader, a nested zip archive will be read. See this page for an example.
using( ZipReader reader = new ZipReader( someStream ) ) { ZipItemLocalHeader header; header = reader.ReadItemLocalHeader(); // Instead of using ReadItemData(), we will get a reading stream to the item's data using( Stream itemStream = reader.GetItemDataStream() ) { // The stream will be 'closed' automatically by the 'using' statement. This will not // close the zip file or the current item. It will only indicate to the stream // that its work is done and clear its resources. // Having a stream object handy here makes using a XmlReader very easy using( XmlReader xml = XmlReader.Create( itemStream ) ) { xml.MoveToContent(); } } header = reader.ReadItemLocalHeader(); using( Stream itemStream = reader.GetItemDataStream() ) { using( XmlReader xml = XmlReader.Create( itemStream ) ) { xml.MoveToContent(); // This is legal reader.ReadItemData( someBuffer, 0, 16 ); xml.Read(); } } // This is also legal reader.ReadItemData( someBuffer, 16, 64 ); }
Using reader As New ZipReader(someStream) Dim header As ZipItemLocalHeader header = reader.ReadItemLocalHeader() ' Instead of using ReadItemData(), we will get a reading stream to the item's data Using itemStream As Stream = reader.GetItemDataStream() ' The stream will be 'closed' automatically by the 'using' statement. This will not ' close the zip file or the current item. It will only indicate to the stream ' that its work is done and clear its resources. ' Having a stream object handy here makes using a XmlReader very easy Using xml As XmlReader = XmlReader.Create(itemStream) xml.MoveToContent() End Using End Using header = reader.ReadItemLocalHeader() Using itemStream As Stream = reader.GetItemDataStream() Using xml As XmlReader = XmlReader.Create(itemStream) xml.MoveToContent() ' This is legal reader.ReadItemData(someBuffer, 0, 16) xml.Read() End Using End Using ' This is also legal reader.ReadItemData(someBuffer, 16, 64) End Using
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2