Xceed .NET Libraries Documentation
Xceed.Zip Assembly / Xceed.Zip.ReaderWriter Namespace / ZipReader Class / GetItemDataStream Method
Example


In This Topic
    GetItemDataStream Method (ZipReader)
    In This Topic
    Returns a new Stream object that implements a read-only stream around the ReadItemData method.
    Syntax
    'Declaration
     
    
    Public Function GetItemDataStream() As Stream
    'Usage
     
    
    Dim instance As ZipReader
    Dim value As Stream
     
    value = instance.GetItemDataStream()
    public Stream GetItemDataStream()

    Return Value

    A read-only System.IO.Stream object.
    Remarks

    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.

    Example
    Creating a ZipReader instance that will read from a stream that has been prepared.
    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
    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
    Supported Frameworks

    .NET: net5.0, net5.0-windows, net6.0, net6.0-macos, net6.0-windows, net7.0, net7.0-macos, net7.0-windows, net8.0, net8.0-browser, net8.0-macos, net8.0-windows, net9.0, net9.0-browser, net9.0-macos, net9.0-windows, net10.0, net10.0-browser, net10.0-macos, net10.0-windows.

    .NET Standard: netstandard2.0, netstandard2.1

    .NET Framework: net20, net35, net40, net403, net45, net451, net452, net46, net461, net462, net463, net47, net471, net472, net48, net481.

    See Also