Back to Devexpress

How to: Compress .NET Stream

officefileapi-15245-zip-compression-and-archive-api-examples-how-to-compress-net-stream.md

latest1.7 KB
Original Source

How to: Compress .NET Stream

  • Sep 19, 2023

To compress a .NET stream, do the following:

  1. Create a ZipArchive class instance.
  2. Call its ZipArchive.AddStream method and specify a source stream.
  3. Call the proper ZipArchive.Save method overload to create an archive and save it to a stream.

View Example

csharp
using DevExpress.Compression;

public void ArchiveStream() {
    using (Stream myStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("DevExpress"))) {
        using (Stream myZippedStream = new FileStream("ArchiveStream.zip", System.IO.FileMode.Create)) {
            using (ZipArchive archive = new ZipArchive()) {
                archive.AddStream("myStream", myStream);
                archive.Save(myZippedStream);
            }
        }
    }
}
vb
Imports DevExpress.Compression

Public Sub ArchiveStream()
    Using myStream As Stream = New MemoryStream(System.Text.Encoding.UTF8.GetBytes("DevExpress"))
        Using myZippedStream As Stream = New FileStream("ArchiveStream.zip", System.IO.FileMode.Create)
            Using archive As New ZipArchive()
                archive.AddStream("myStream", myStream)
                archive.Save(myZippedStream)
            End Using
        End Using
    End Using
End Sub