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.
staticvoid 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 = newbyte[] { 12, 45, 67, 35, 67, 255, 255, 45 };
// This is legal
writer.WriteItemData( someData );
xml.WriteEndElement();
xml.WriteEndDocument();
}
}
byte[] someOtherData = newbyte[] { 6, 34, 56, 108, 127, 98, 44, 35, 67, 255, 255, 45 };
// This is also legal
writer.WriteItemData( someOtherData );
}
}
}
PrivateSharedSub Example()
Dim zipFilePath AsString = "D:\RealTimeZipExamples\MyZipFile.zip"' Create a stream for a new zip file
Using zipFileStream AsNew FileStream(zipFilePath, FileMode.Create, FileAccess.Write, FileShare.None)
' Create a ZipWriter instance that will write into a stream that has been prepared
Using writer AsNew ZipWriter(zipFileStream)
Dim header AsNew 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()
EndUsingEndUsing
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() AsByte = {12, 45, 67, 35, 67, 255, 255, 45}
' This is legal
writer.WriteItemData(someData)
xml.WriteEndElement()
xml.WriteEndDocument()
EndUsingEndUsingDim someOtherData() AsByte = {6, 34, 56, 108, 127, 98, 44, 35, 67, 255, 255, 45}
' This is also legal
writer.WriteItemData(someOtherData)
EndUsingEndUsingEnd Sub