Xceed .NET Libraries Documentation
Welcome to Xceed Data Manipulation Compoents for .NET and .NET Standard / Basic Concepts / Zip and streaming capabilities / Streaming compression / Compressing / Compressing a byte array

In This Topic
    Compressing a byte array
    In This Topic

    This topic demonstrates how to compress a byte array using the static Compress method of the XceedCompressedStream class. 

    For the purposes of this example we used the Compress method of XceedCompressedStream class; however, we could have also used the Compress method of the GZipCompressedStream or the ZLibCompressedStream classes.

    Basic steps

    To compress a byte array, the following steps must be taken:

    1. Retrieve a byte array containing the data to compress. For the purposes of this example, we will convert a string to a byte array using the System.Text.Encoding.GetBytes() method.
    2. Compress the data. The resulting compressed data will be returned as a byte array.

    Example

    The following example demonstrates how to compress an array of bytes using the static Compress method of the XceedCompressedStream class.

    using System.IO;
    using System.Text;
    
    using Xceed.Compression;
    using Xceed.Compression.Formats;
    
    namespace FileSystemDocumentationExamples.CompressionComponent
    {
      public class CompressArrayXceedCompressedStreamExample
      {
        public void MyMainMethod()
        {
          /* Because Xceed.Compression.Formats is an optional assembly not automatically
             unlocked by Xceed Zip for .NET, we must explicitly set our license key to it
             before use or else an exception will be thrown. */
          Xceed.Compression.Formats.Licenser.LicenseKey = "<your Xceed Zip for .NET license key>";
        }
    
        public void Example()
        {
          // Generate a byte array from a string
          byte[] data = Encoding.UTF8.GetBytes( "This is the data to compress compress compress" );
    
          /* Optional: the compression method and level can be specified. */
          CompressionMethod compressionMethod = CompressionMethod.Deflated;
          CompressionLevel compressionLevel = CompressionLevel.Normal;
          string password = String.Empty;
    
          // Optional: the data will be encrypted if a non-empty password is specified
          //password = "password";
    
          // Optional: Data can be selected for compression at a specific offset and for a specific size
          int offset = 0;
          int count = data.Length;
    
          // Compress the data
          byte[] compressedData = XceedCompressedStream.Compress( data, offset, count, compressionMethod, compressionLevel, password );
        }
      }
    }
    Imports System.IO
    Imports System.Text
    
    Imports Xceed.Compression
    Imports Xceed.Compression.Formats
    
    Namespace FileSystemDocumentationExamples.CompressionComponent
      Public Class CompressArrayXceedCompressedStreamExample
        Public Sub MyMainMethod()
    '       Because Xceed.Compression.Formats is an optional assembly not automatically
    '         unlocked by Xceed Zip for .NET, we must explicitly set our license key to it
    '         before use or else an exception will be thrown. 
          Xceed.Compression.Formats.Licenser.LicenseKey = "<your Xceed Zip for .NET license key>"
        End Sub
    
        Public Sub Example()
          ' Generate a byte array from a string
          Dim data() As Byte = Encoding.UTF8.GetBytes("This is the data to compress compress compress")
    
          ' Optional: the compression method and level can be specified. 
          Dim compressionMethod As CompressionMethod = CompressionMethod.Deflated
          Dim compressionLevel As CompressionLevel = CompressionLevel.Normal
          Dim password As String = String.Empty
    
          ' Optional: the data will be encrypted if a non-empty password is specified
          'password = "password";
    
          ' Optional: Data can be selected for compression at a specific offset and for a specific size
          Dim offset As Integer = 0
          Dim count As Integer = data.Length
    
          ' Compress the data
          Dim compressedData() As Byte = XceedCompressedStream.Compress(data, offset, count, compressionMethod, compressionLevel, password)
        End Sub
      End Class
    End Namespace

    Things you should consider

    The main questions you should ask yourself when compressing data read from a stream are: