Xceed .NET Libraries Documentation
GetItemDataStream Method (ZipWriter)
Example 


Xceed.Zip Assembly > Xceed.Zip.ReaderWriter Namespace > ZipWriter Class : GetItemDataStream Method
Returns a new Stream object that implements a write-only stream around the WriteItemData method.
Syntax
'Declaration
 
Public Function GetItemDataStream() As Stream
'Usage
 
Dim instance As ZipWriter

Dim value As Stream

 

value = instance.GetItemDataStream()
public Stream GetItemDataStream()

Return Value

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

The method creates a new write-only, non-seeking stream object based on the ZipWriter instance's current item. Each call to the stream's Stream.Write method will call WriteItemData.

The method allows you to use the Stream class interface to write item data into ZipWriter instead of using WriteItemData directly. This makes it possible to integrate ZipWriter 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 written.

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.

WriteItemData can still be called to write 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 create nested zip archives with ZipWriter. 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 ZipWriter, a nested zip archive will be created. See this page for an example.

Example
The following examples show how to create a ZipWriter instance that will write into a stream that has been prepared.
// Create a ZipWriter instance that will write into a stream that has been prepared

using( ZipWriter writer = new ZipWriter( someStream ) ) 

{ 

  ZipItemLocalHeader header = new ZipItemLocalHeader(); 



  header.FileName = "File1.xml"; 

  writer.WriteItemLocalHeader( header ); 



  // Instead of using WriteItemData(), we will get a writing stream to the item's data 

  using( Stream itemStream = writer.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 XmlWriter very easy 

    using( XmlWriter xml = XmlWriter.Create( itemStream ) ) 

    { 

      xml.WriteStartDocument(); 

      xml.WriteStartElement( "SomeType" ); 

      xml.WriteElementString( "ID", "Something" ); 

      xml.WriteEndElement(); 

      xml.WriteEndDocument(); 

    } 

  } 



  header.FileName = "File2.xml"; 

  writer.WriteItemLocalHeader( header ); 

  using( Stream itemStream = writer.GetItemDataStream() ) 

  { 

    using( XmlWriter xml = XmlWriter.Create( itemStream ) ) 

    { 

      xml.WriteStartDocument(); 

      xml.WriteStartElement( "SomeOtherType" ); 

       

      // This is legal 

      writer.WriteItemData( someData ); 



      xml.WriteEndElement(); 

      xml.WriteEndDocument(); 

    } 

  } 

   

  // This is also legal 

  writer.WriteItemData( someData ); 

}
' Create a ZipWriter instance that will write into a stream that has been prepared 

Using writer As New ZipWriter(someStream) 

 Dim header As New ZipItemLocalHeader() 



 header.FileName = "File1.xml" 

 writer.WriteItemLocalHeader(header) 



 ' Instead of using WriteItemData(), we will get a writing stream to the item's data 

 Using itemStream As Stream = writer.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 XmlWriter very easy 

   Using xml As XmlWriter = XmlWriter.Create(itemStream) 

     xml.WriteStartDocument() 

     xml.WriteStartElement("SomeType") 

     xml.WriteElementString("ID", "Something") 

     xml.WriteEndElement() 

     xml.WriteEndDocument() 

   End Using 

 End Using 



 header.FileName = "File2.xml" 

 writer.WriteItemLocalHeader(header) 

 Using itemStream As Stream = writer.GetItemDataStream() 

   Using xml As XmlWriter = XmlWriter.Create(itemStream) 

     xml.WriteStartDocument() 

     xml.WriteStartElement("SomeOtherType") 



     ' This is legal 

     writer.WriteItemData(someData) 



     xml.WriteEndElement() 

     xml.WriteEndDocument() 

   End Using 

 End Using 



 ' This is also legal 

 writer.WriteItemData(someData) 

End Using
Requirements

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

See Also

Reference

ZipWriter Class
ZipWriter Members