The following example demonstrates how to create a Zip archive using MemoryStream objects. The usage is the same as it would be on desktop .NET. MemoryStream objects are used to maintain focus on the ZipWriter API.
The MemoryStream objects can be changed to any stream objects that derive from the System.IO.Stream. NetworkStream, FileStream, etc.
static Stream ZipWriterExampleXamarin() { /* NOTE: The zip file can be any type of stream you need. A network stream, a file stream, etc The component will never call Stream.Seek() on the stream and will only write to it. */ // Create a writable stream for the zip file Stream zipFileStream = new MemoryStream(); /* Wrapping the ZipWriter object in a 'using' block will make sure the archive is closed properly after we're done adding content. If you use ZipWriter outside a 'using' block, make sure you call ZipWriter.CloseZipFile() to flush the archive's meta data to the output stream. */ // Create the ZipWriter object around the stream using( ZipWriter zipWriter = new ZipWriter( zipFileStream ) ) { /* As a convenience, ZipWriter offers the option to automatically close the output stream when ZipWriter is closed. The behavior is disabled by default. In this example, we will not enable it as we want to manipulate the zip file after it has been written. */ //zipWriter.AllowOutputStreamClosure = true; /* If we'll be adding many items in the archive, we'll create a work buffer ahead of time and use it over and over again to avoid creating a new buffer each time we read from a source file. This example doesn't add many items but still shows the technique. */ int bufferSize = 64 * 1024; byte[] buffer = new byte[ bufferSize ]; /* Create a local header object that will be used to specify the path/filename in the zip file and other options like compression and encryption. */ ZipItemLocalHeader localHeader = new ZipItemLocalHeader(); /* Options can be set "globally" before zipping starts. We will set the file name for each file inside the loop. The other options have default values that satisfy us: The compression method is Deflated at a the 'Normal' compression level No encryption. No comment. */ //localHeader.CompressionMethod = CompressionMethod.Deflated; //localHeader.CompressionLevel = CompressionLevel.Normal; //localHeader.EncryptionMethod = EncryptionMethod.WinZipAes; //localHeader.EncryptionPassword = "password"; // Assign the filename in zip in the header localHeader.FileName = "MyFile1.dat"; // Optional. Set the last write date/time in the header localHeader.LastWriteDateTime = DateTime.Now; /* NOTE: The source data can be any type of stream you need. A network stream, a file stream, etc The component will never call Stream.Seek() on the stream and will only read from it. This example will use memory data to keep it simple and on point. */ // Create some source data byte[] dataToZip = System.Text.Encoding.UTF8.GetBytes( "The quick brown fox jumps over the lazy dog" ); // Create a stream around the source data using( Stream sourceDataStream = new MemoryStream( dataToZip, false ) ) { // Write the local header for the file in the archive zipWriter.WriteItemLocalHeader( localHeader ); // Write the entire stream's content to the archive, using the work buffer we created zipWriter.WriteItemData( sourceDataStream, buffer, 0, bufferSize ); } } /* At this point, 'zipFileStream' contains a zip file. You can store it, upload it, etc as you see fit. Because it is a stream, remember to change the current position to the start of the stream if you wish to read the data. */ zipFileStream.Seek( 0, SeekOrigin.Begin ); /* TODO: Send/store/whatever the zip file */ return zipFileStream; }
Private Shared Function ZipWriterExampleXamarin() As Stream ' NOTE: The zip file can be any type of stream you need. A network stream, a file stream, etc ' The component will never call Stream.Seek() on the stream and will only write to it. ' Create a writable stream for the zip file Dim zipFileStream As Stream = New MemoryStream() ' Wrapping the ZipWriter object in a 'using' block will make sure the archive is closed ' properly after we're done adding content. If you use ZipWriter outside a 'using' block, ' make sure you call ZipWriter.CloseZipFile() to flush the archive's meta data ' to the output stream. ' Create the ZipWriter object around the stream Using zipWriter As New ZipWriter(zipFileStream) ' As a convenience, ZipWriter offers the option to automatically close the output stream when ' ZipWriter is closed. The behavior is disabled by default. ' ' In this example, we will not enable it as we want to manipulate the zip file ' after it has been written. 'zipWriter.AllowOutputStreamClosure = true; ' If we'll be adding many items in the archive, we'll create a work buffer ' ahead of time and use it over and over again to avoid creating a new buffer ' each time we read from a source file. ' ' This example doesn't add many items but still shows the technique. Dim bufferSize As Integer = 64 * 1024 Dim buffer(bufferSize - 1) As Byte ' Create a local header object that will be used to specify the path/filename in ' the zip file and other options like compression and encryption. Dim localHeader As New ZipItemLocalHeader() ' Options can be set "globally" before zipping starts. ' We will set the file name for each file inside the loop. The other ' options have default values that satisfy us: ' The compression method is Deflated at a the 'Normal' compression level ' No encryption. ' No comment. 'localHeader.CompressionMethod = CompressionMethod.Deflated; 'localHeader.CompressionLevel = CompressionLevel.Normal; 'localHeader.EncryptionMethod = EncryptionMethod.WinZipAes; 'localHeader.EncryptionPassword = "password"; ' Assign the filename in zip in the header localHeader.FileName = "MyFile1.dat" ' Optional. Set the last write date/time in the header localHeader.LastWriteDateTime = DateTime.Now ' NOTE: The source data can be any type of stream you need. A network stream, a file stream, etc ' The component will never call Stream.Seek() on the stream and will only read from it. ' ' This example will use memory data to keep it simple and on point. ' Create some source data Dim dataToZip() As Byte = System.Text.Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog") ' Create a stream around the source data Using sourceDataStream As Stream = New MemoryStream(dataToZip, False) ' Write the local header for the file in the archive zipWriter.WriteItemLocalHeader(localHeader) ' Write the entire stream's content to the archive, using the work buffer we created zipWriter.WriteItemData(sourceDataStream, buffer, 0, bufferSize) End Using End Using ' At this point, 'zipFileStream' contains a zip file. You can store it, upload it, etc as you ' see fit. Because it is a stream, remember to change the current position to the start of the ' stream if you wish to read the data. zipFileStream.Seek(0, SeekOrigin.Begin) ' TODO: Send/store/whatever the zip file Return zipFileStream End Function