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:
- 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.
- 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.
Things you should consider
The main questions you should ask yourself when compressing data read from a stream are:
- Do you want to compress the byte array using the Xceed or ZLib compression formats? Use the static Compress method of the GZipCompressedStream or ZLibCompressedStream classes instead of the XceedCompressedStream's.
- Do you want to compress the byte array but without a compression format (header/footer)? Use the Compress method of the QuickCompression class.
- Do you want to compress data as it is written to a stream? Use the Write method of the desired compression format class.
- Do you want to compress a string? Convert it to a byte array before passing it to the Compress method using the GetBytes method of the .NET Framework's System.Text.Encoding class.
- Do you need to convert the resulting compressed byte array to a string? Use the GetString method of the .NET Framework's System.Text.Encoding class.