Class CompressionHelper
Provides helper methods for compressing and decompressing data using various algorithms.
public static class CompressionHelper
- Inheritance
-
CompressionHelper
- Inherited Members
Fields
DefaultBufferSize
Represents the default buffer size used in compression and decompression operations.
public const int DefaultBufferSize = 81920
Field Value
Methods
CompressAsync<TCompressStream>(ArraySegment<byte>, CompressionLevel, int)
Asynchronously compresses a segment of a byte array using the specified compression stream.
public static Task<byte[]> CompressAsync<TCompressStream>(this ArraySegment<byte> v, CompressionLevel level = CompressionLevel.Optimal, int bufferSize = 81920) where TCompressStream : Stream
Parameters
vArraySegment<byte>The segment of the byte array to compress.
levelCompressionLevelThe compression level to use.
bufferSizeintThe buffer size to use during compression.
Returns
- Task<byte[]>
A task representing the asynchronous operation, with a compressed byte array as the result.
Type Parameters
TCompressStreamThe type of compression stream to use.
CompressAsync<TCompressStream>(byte[], int?, int?, CompressionLevel, int, CancellationToken)
Asynchronously compresses a portion of a byte array using the specified compression stream.
public static Task<byte[]> CompressAsync<TCompressStream>(this byte[] input, int? index = null, int? count = null, CompressionLevel level = CompressionLevel.Optimal, int bufferSize = 81920, CancellationToken cancellationToken = default) where TCompressStream : Stream
Parameters
inputbyte[]The byte array to compress.
indexint?The starting index for compression.
countint?The number of bytes to compress.
levelCompressionLevelThe compression level to use.
bufferSizeintThe buffer size to use during compression.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task<byte[]>
A task representing the asynchronous operation, with a compressed byte array as the result.
Type Parameters
TCompressStreamThe type of compression stream to use.
CompressAsync<TCompressStream>(Stream, Stream, CompressionLevel, bool, int, CancellationToken)
Asynchronously compresses data from the input stream and writes the compressed data to the output stream using the specified compression stream.
public static Task CompressAsync<TCompressStream>(this Stream input, Stream output, CompressionLevel level = CompressionLevel.Optimal, bool leaveOpen = true, int bufferSize = 81920, CancellationToken cancellationToken = default) where TCompressStream : Stream
Parameters
inputStreamThe input stream containing data to compress.
outputStreamThe output stream to write compressed data to.
levelCompressionLevelThe compression level to use.
leaveOpenboolWhether to leave the output stream open after compression.
bufferSizeintThe buffer size to use during compression.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task
A task representing the asynchronous compression operation.
Type Parameters
TCompressStreamThe type of compression stream to use.
Compress<TCompressStream>(byte[], int?, int?, CompressionLevel, int)
Compresses a portion of a byte array using the specified compression stream.
public static byte[] Compress<TCompressStream>(this byte[] input, int? index = null, int? count = null, CompressionLevel level = CompressionLevel.Optimal, int bufferSize = 81920) where TCompressStream : Stream
Parameters
inputbyte[]The byte array to compress.
indexint?The starting index for compression.
countint?The number of bytes to compress.
levelCompressionLevelThe compression level to use.
bufferSizeintThe buffer size to use during compression.
Returns
- byte[]
A compressed byte array.
Type Parameters
TCompressStreamThe type of compression stream to use.
DeflateFrom(ArraySegment<byte>)
Compresses a segment of a byte array using the Deflate algorithm.
public static byte[] DeflateFrom(this ArraySegment<byte> v)
Parameters
vArraySegment<byte>The byte array segment to compress.
Returns
- byte[]
A compressed byte array.
DeflateFrom(byte[], int?, int?, int)
Compresses a portion of a byte array using the Deflate algorithm.
public static byte[] DeflateFrom(this byte[] input, int? index = null, int? count = null, int bufferSize = 81920)
Parameters
inputbyte[]The byte array to compress.
indexint?The starting index from which to begin compression.
countint?The number of bytes to compress.
bufferSizeintThe buffer size to use during compression.
Returns
- byte[]
A compressed byte array.
DeflateTo(byte[])
Compresses the specified byte array using the Deflate algorithm.
public static byte[] DeflateTo(this byte[] input)
Parameters
inputbyte[]The byte array to compress.
Returns
- byte[]
A compressed byte array.
Do7Zip(byte[])
Compresses the specified byte array using the 7Zip (LZMA) algorithm.
public static byte[] Do7Zip(this byte[] input)
Parameters
inputbyte[]The byte array to compress.
Returns
- byte[]
A compressed byte array.
Un7Zip(byte[])
Decompresses a 7Zip (LZMA) compressed byte array.
public static byte[] Un7Zip(this byte[] input)
Parameters
inputbyte[]The byte array containing 7Zip-compressed data.
Returns
- byte[]
A decompressed byte array.
UnDeflate(ArraySegment<byte>)
Decompresses a segment of Deflate-compressed bytes into a UTF-8 encoded string.
public static string UnDeflate(this ArraySegment<byte> v)
Parameters
vArraySegment<byte>The byte array segment containing Deflate-compressed data.
Returns
- string
A string resulting from decompression.
UnDeflate(ArraySegment<byte>, byte[])
Decompresses a Deflate-compressed segment of bytes into the provided destination buffer.
public static int UnDeflate(this ArraySegment<byte> input, byte[] destination)
Parameters
inputArraySegment<byte>The byte array segment containing Deflate-compressed data.
destinationbyte[]The buffer to store decompressed data.
Returns
- int
The number of bytes written into the destination buffer.
UnDeflate(byte[])
Decompresses a Deflate-compressed byte array into a UTF-8 encoded string.
public static string UnDeflate(this byte[] input)
Parameters
inputbyte[]The byte array containing Deflate-compressed data.
Returns
- string
A string resulting from decompression.
UnDeflate(byte[], int, int)
Decompresses a portion of a Deflate-compressed byte array into a UTF-8 encoded string.
public static string UnDeflate(this byte[] input, int index, int count)
Parameters
inputbyte[]The byte array containing Deflate-compressed data.
indexintThe starting index for decompression.
countintThe number of bytes to decompress.
Returns
- string
A string resulting from decompression.
UnDeflate(byte[], int, int, byte[])
Decompresses a portion of a Deflate-compressed byte array into the provided destination buffer.
public static int UnDeflate(this byte[] input, int index, int count, byte[] destination)
Parameters
inputbyte[]The byte array containing Deflate-compressed data.
indexintThe starting index for decompression.
countintThe number of bytes to decompress.
destinationbyte[]The buffer to store decompressed data.
Returns
- int
The number of bytes written into the destination buffer.
UnGZip(ArraySegment<byte>)
Decompresses a GZip-compressed segment of bytes into a UTF-8 encoded string.
public static string UnGZip(this ArraySegment<byte> v)
Parameters
vArraySegment<byte>The byte array segment containing GZip-compressed data.
Returns
- string
A string resulting from decompression.
UnGZip(ArraySegment<byte>, byte[])
Decompresses a GZip-compressed segment of bytes into the provided destination buffer.
public static int UnGZip(this ArraySegment<byte> input, byte[] destination)
Parameters
inputArraySegment<byte>The byte array segment containing GZip-compressed data.
destinationbyte[]The buffer to store decompressed data.
Returns
- int
The number of bytes written into the destination buffer.
UnGZip(byte[])
Decompresses a GZip-compressed byte array into a UTF-8 encoded string.
public static string UnGZip(this byte[] input)
Parameters
inputbyte[]The byte array containing GZip-compressed data.
Returns
- string
A string resulting from decompression.
UnGZip(byte[], int, int)
Decompresses a GZip-compressed portion of a byte array into a UTF-8 encoded string.
public static string UnGZip(this byte[] input, int index, int count)
Parameters
inputbyte[]The byte array containing GZip-compressed data.
indexintThe starting index from which to begin decompression.
countintThe number of bytes to decompress.
Returns
- string
A string resulting from decompression.
UnGZip(byte[], int, int, byte[])
Decompresses a portion of a GZip-compressed byte array into the provided destination buffer.
public static int UnGZip(this byte[] input, int index, int count, byte[] destination)
Parameters
inputbyte[]The byte array containing GZip-compressed data.
indexintThe starting index for decompression.
countintThe number of bytes to decompress.
destinationbyte[]The buffer to store decompressed data.
Returns
- int
The number of bytes written into the destination buffer.
UncompressAsync<TCompressStream>(byte[], int?, int?, int, CancellationToken)
Asynchronously decompresses a portion of a byte array using the specified compression stream.
public static Task<byte[]> UncompressAsync<TCompressStream>(this byte[] input, int? index = null, int? count = null, int bufferSize = 81920, CancellationToken cancellationToken = default) where TCompressStream : Stream
Parameters
inputbyte[]The byte array containing compressed data.
indexint?The starting index for decompression.
countint?The number of bytes to decompress.
bufferSizeintThe buffer size to use during decompression.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task<byte[]>
A task representing the asynchronous operation, with a decompressed byte array as the result.
Type Parameters
TCompressStreamThe type of compression stream to use.
UncompressAsync<TCompressStream>(Stream, Stream, bool, int, CancellationToken)
Asynchronously decompresses data from the input stream and writes the decompressed data to the output stream using the specified compression stream.
public static Task UncompressAsync<TCompressStream>(this Stream input, Stream output, bool leaveOpen = true, int bufferSize = 81920, CancellationToken cancellationToken = default) where TCompressStream : Stream
Parameters
inputStreamThe input stream containing compressed data.
outputStreamThe output stream to write decompressed data to.
leaveOpenboolWhether to leave the input stream open after decompression.
bufferSizeintThe buffer size to use during decompression.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task
A task representing the asynchronous decompression operation.
Type Parameters
TCompressStreamThe type of compression stream to use.
Uncompress<TCompressStream>(byte[], int?, int?, int)
Decompresses a portion of a byte array using the specified compression stream.
public static byte[] Uncompress<TCompressStream>(this byte[] input, int? index = null, int? count = null, int bufferSize = 81920) where TCompressStream : Stream
Parameters
inputbyte[]The byte array containing compressed data.
indexint?The starting index for decompression.
countint?The number of bytes to decompress.
bufferSizeintThe buffer size to use during decompression.
Returns
- byte[]
A decompressed byte array.
Type Parameters
TCompressStreamThe type of compression stream to use.
Unzip(byte[], bool, Func<string, bool>)
Extracts entries from a ZIP archive contained in the specified byte array.
public static IEnumerable<(string name, Stream body)> Unzip(this byte[] input, bool leaveOpen = false, Func<string, bool> filter = null)
Parameters
inputbyte[]The byte array containing the ZIP archive.
leaveOpenboolWhether to leave the underlying stream open.
filterFunc<string, bool>A function to filter entries by name. Only entries returning true will be processed.
Returns
- IEnumerable<(string name, Stream body)>
An enumerable of tuples with the entry name and its content stream.
Unzip(Stream, bool, Func<string, bool>)
Extracts entries from a ZIP archive contained in the specified stream.
public static IEnumerable<(string name, Stream body)> Unzip(this Stream input, bool leaveOpen = false, Func<string, bool> filter = null)
Parameters
inputStreamThe stream containing the ZIP archive.
leaveOpenboolWhether to leave the underlying stream open after processing.
filterFunc<string, bool>A function to filter entries by name. Only entries returning true will be processed.
Returns
- IEnumerable<(string name, Stream body)>
An enumerable of tuples with the entry full name and its content stream.