Xceed .NET Libraries Documentation
Decompressing a byte array

Welcome to Xceed .NET, .NET Standard and Xamarin Libraries! > Basic Concepts > Zip and streaming capabilities > Streaming compression > Decompressing > Decompressing a byte array

This topic demonstrates how to decompress a GZip compressed byte array using the static Decompress method of the XceedCompressedStream class.

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

Basic steps

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

  1. Retrieve a byte array containing the compressed data. For the purposes of this example, our compressed data is the data compressed using the Compress example.
  2. Decompress 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 Decompress method of the GZipCompressedStream class.

using System.IO;
using System.Text;

using Xceed.Compression;
using Xceed.Compression.Formats;

namespace FileSystemDocumentationExamples.CompressionComponent
{
  public class DecompressArrayXceedCompressedStreamExample
  {
    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( byte[] compressedData )
    {
      /* 'compressedData' is assumed to contain the compressed data created by XceedCompressedStream.Compress() */

      // Optional: If the data was encrypted, supply the password
      string password = String.Empty;
      password = "password";

      // Optional: Data can be selected for decompression from the source array at a specific offset and for a specific size
      int offset = 0;
      int count = compressedData.Length;

      // Decompress the data
      byte[] uncompressedData = XceedCompressedStream.Decompress( compressedData, offset, count, password );

      /* In our compress example, we generated the source data from a string. At this point,
         we can interpret the uncompressed data as a string. */
      
      string sourceData = Encoding.UTF8.GetString( uncompressedData );
    }
  }
}
Imports System.IO
Imports System.Text

Imports Xceed.Compression
Imports Xceed.Compression.Formats

Namespace FileSystemDocumentationExamples.CompressionComponent
  Public Class DecompressArrayXceedCompressedStreamExample
    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(ByVal compressedData() As Byte)
      ' 'compressedData' is assumed to contain the compressed data created by XceedCompressedStream.Compress() 

      ' Optional: If the data was encrypted, supply the password
      Dim password As String = String.Empty
      password = "password"

      ' Optional: Data can be selected for decompression from the source array at a specific offset and for a specific size
      Dim offset As Integer = 0
      Dim count As Integer = compressedData.Length

      ' Decompress the data
      Dim uncompressedData() As Byte = XceedCompressedStream.Decompress(compressedData, offset, count, password)

'       In our compress example, we generated the source data from a string. At this point,
'         we can interpret the uncompressed data as a string. 

      Dim sourceData As String = Encoding.UTF8.GetString(uncompressedData)
    End Sub
  End Class
End Namespace

Things you should consider

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