Skip to content

Create a Zip from to a memory stream or byte array

nils måsén edited this page Aug 21, 2019 · 2 revisions

Code Reference / Zip Samples / Create a Zip from to a memory stream or byte array

This sample concentrates on the differences for memorystream output, the most important of which is setting IsStreamOwner = false so that the Close (which is needed to finish up the output) does not close the underlying memorystream. For multiple entries, passwords, etc, see the code sample above.

C#

using ICSharpCode.SharpZipLib.Zip;

// Compresses the supplied memory stream, naming it as zipEntryName, into a zip,
// which is returned as a memory stream or a byte array.
public MemoryStream CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName) {

    var outputMemStream = new MemoryStream();
    using(var zipStream = new ZipOutputStream(outputMemStream)){

        // 0-9, 9 being the highest level of compression
        zipStream.SetLevel(3); 

        ZipEntry newEntry = new ZipEntry(zipEntryName);
        newEntry.DateTime = DateTime.Now;

        zipStream.PutNextEntry(newEntry);

        StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
        zipStream.CloseEntry();

        // Stop ZipStream.Dispose() from also Closing the underlying stream.
        zipStream.IsStreamOwner = false;	
    }

    outputMemStream.Position = 0;
    return outputMemStream;

    // Alternative outputs:
    // ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
    byte[] byteArrayOut = outputMemStream.ToArray();

    // GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
    byte[] byteArrayOut = outputMemStream.GetBuffer();
    long len = outputMemStream.Length;

}

Visual Basic

Imports ICSharpCode.SharpZipLib.Zip

' Compresses the supplied memory stream, naming it as zipEntryName, into a zip,
' which is returned as a memory stream or a byte array.
'
Public Function CreateToMemoryStream(memStreamIn As MemoryStream, zipEntryName As String) As MemoryStream

    Dim outputMemStream As New MemoryStream()
    Dim zipStream As New ZipOutputStream(outputMemStream)

    zipStream.SetLevel(3)		'0-9, 9 being the highest level of compression
    Dim newEntry As New ZipEntry(zipEntryName)
    newEntry.DateTime = DateTime.Now

    zipStream.PutNextEntry(newEntry)

    StreamUtils.Copy(memStreamIn, zipStream, New Byte(4095) {})
    zipStream.CloseEntry()

    zipStream.IsStreamOwner = False		' False stops the Close also Closing the underlying stream.
    zipStream.Close()			' Must finish the ZipOutputStream before using outputMemStream.
    outputMemStream.Position = 0
    Return outputMemStream

    ' Alternative outputs:
    ' ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
    Dim byteArrayOut As Byte() = outputMemStream.ToArray()

    ' GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
    Dim byteArrayOut As Byte() = outputMemStream.GetBuffer()
    Dim len As Long = outputMemStream.Length
End Function