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