Back to Devexpress

How to: Compress Byte Array

officefileapi-15246-zip-compression-and-archive-api-examples-how-to-compress-byte-array.md

latest1.8 KB
Original Source

How to: Compress Byte Array

  • Sep 19, 2023

To compress a byte array, do the following:

  1. Create a ZipArchive class instance.
  2. Call its ZipArchive.AddByteArray method to specify a byte array to compress.
  3. Call the ZipArchive.Save method to create an archive and save it to a stream.

This code snippet adds a byte array to an archive as an item with the name “myByteArray” and outputs zipped data to the stream.

View Example

csharp
using DevExpress.Compression;

public void ArchiveByteArray() {
    byte[] myByteArray = Enumerable.Repeat((byte)0x78, 10000).ToArray();
    using (Stream myZippedStream = new FileStream("ArchiveByteArray.zip", FileMode.Create)) {
        using (ZipArchive archive = new ZipArchive()) {
            archive.AddByteArray("myByteArray", myByteArray);
            archive.Save(myZippedStream);
        }
    }
}
vb
Imports DevExpress.Compression
        Public Sub ArchiveByteArray()
            Dim myByteArray() As Byte = Enumerable.Repeat(CByte(&H78), 10000).ToArray()
            Using myZippedStream As Stream = New FileStream("ArchiveByteArray.zip", FileMode.Create)
                Using archive As New ZipArchive()
                    archive.AddByteArray("myByteArray", myByteArray)
                    archive.Save(myZippedStream)
                End Using
            End Using
        End Sub