Xceed Real-Time Zip for .NET Documentation
Welcome to Xceed Real-Time Zip for .NET, .NET Standard & Xamarin / Task-Based Help / Writing zipped items using the Stream interface

In This Topic
    Writing zipped items using the Stream interface
    In This Topic

    When writing zipped items, it is sometimes useful to have a System.IO.Stream to give to other objects for them to write the data to be compressed. Classes outside of the application's own code will likely not know about ZipWriter and its WriteItemData() method.

    The GetItemDataStream Method creates a write-only stream that calls WriteItemData() each time the stream's Write() method is called. This allows data to be fed to ZipWriter by objects that require a Stream object for output. The following example show how this can be used.

    static void Example()
    {
      string zipFilePath = @"D:\RealTimeZipExamples\MyZipFile.zip";
    
      // Create a stream for a new zip file
      using( FileStream zipFileStream = new FileStream( zipFilePath, FileMode.Create, FileAccess.Write, FileShare.None ) )
      {
        // Create a ZipWriter instance that will write into a stream that has been prepared
        using( ZipWriter writer = new ZipWriter( zipFileStream ) )
        {
          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" );
    
              byte[] someData = new byte[] { 12, 45, 67, 35, 67, 255, 255, 45 };
    
              // This is legal 
              writer.WriteItemData( someData );
    
              xml.WriteEndElement();
              xml.WriteEndDocument();
            }
          }
    
          byte[] someOtherData = new byte[] { 6, 34, 56, 108, 127, 98, 44, 35, 67, 255, 255, 45 };
    
          // This is also legal 
          writer.WriteItemData( someOtherData );
        }
      }
    }
    Private Shared Sub Example()
      Dim zipFilePath As String = "D:\RealTimeZipExamples\MyZipFile.zip"
    
      ' Create a stream for a new zip file
      Using zipFileStream As New FileStream(zipFilePath, FileMode.Create, FileAccess.Write, FileShare.None)
        ' Create a ZipWriter instance that will write into a stream that has been prepared
        Using writer As New ZipWriter(zipFileStream)
          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")
    
              Dim someData() As Byte = {12, 45, 67, 35, 67, 255, 255, 45}
    
              ' This is legal 
              writer.WriteItemData(someData)
    
              xml.WriteEndElement()
              xml.WriteEndDocument()
            End Using
          End Using
    
          Dim someOtherData() As Byte = {6, 34, 56, 108, 127, 98, 44, 35, 67, 255, 255, 45}
    
          ' This is also legal 
          writer.WriteItemData(someOtherData)
        End Using
      End Using
    End Sub