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
v
ArraySegment<byte>The segment of the byte array to compress.
level
CompressionLevelThe compression level to use.
bufferSize
intThe 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
TCompressStream
The 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
input
byte[]The byte array to compress.
index
int?The starting index for compression.
count
int?The number of bytes to compress.
level
CompressionLevelThe compression level to use.
bufferSize
intThe buffer size to use during compression.
cancellationToken
CancellationTokenA 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
TCompressStream
The 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
input
StreamThe input stream containing data to compress.
output
StreamThe output stream to write compressed data to.
level
CompressionLevelThe compression level to use.
leaveOpen
boolWhether to leave the output stream open after compression.
bufferSize
intThe buffer size to use during compression.
cancellationToken
CancellationTokenA token to monitor for cancellation requests.
Returns
- Task
A task representing the asynchronous compression operation.
Type Parameters
TCompressStream
The 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
input
byte[]The byte array to compress.
index
int?The starting index for compression.
count
int?The number of bytes to compress.
level
CompressionLevelThe compression level to use.
bufferSize
intThe buffer size to use during compression.
Returns
- byte[]
A compressed byte array.
Type Parameters
TCompressStream
The 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
v
ArraySegment<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
input
byte[]The byte array to compress.
index
int?The starting index from which to begin compression.
count
int?The number of bytes to compress.
bufferSize
intThe 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
input
byte[]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
input
byte[]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
input
byte[]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
v
ArraySegment<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
input
ArraySegment<byte>The byte array segment containing Deflate-compressed data.
destination
byte[]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
input
byte[]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
input
byte[]The byte array containing Deflate-compressed data.
index
intThe starting index for decompression.
count
intThe 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
input
byte[]The byte array containing Deflate-compressed data.
index
intThe starting index for decompression.
count
intThe number of bytes to decompress.
destination
byte[]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
v
ArraySegment<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
input
ArraySegment<byte>The byte array segment containing GZip-compressed data.
destination
byte[]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
input
byte[]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
input
byte[]The byte array containing GZip-compressed data.
index
intThe starting index from which to begin decompression.
count
intThe 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
input
byte[]The byte array containing GZip-compressed data.
index
intThe starting index for decompression.
count
intThe number of bytes to decompress.
destination
byte[]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
input
byte[]The byte array containing compressed data.
index
int?The starting index for decompression.
count
int?The number of bytes to decompress.
bufferSize
intThe buffer size to use during decompression.
cancellationToken
CancellationTokenA 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
TCompressStream
The 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
input
StreamThe input stream containing compressed data.
output
StreamThe output stream to write decompressed data to.
leaveOpen
boolWhether to leave the input stream open after decompression.
bufferSize
intThe buffer size to use during decompression.
cancellationToken
CancellationTokenA token to monitor for cancellation requests.
Returns
- Task
A task representing the asynchronous decompression operation.
Type Parameters
TCompressStream
The 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
input
byte[]The byte array containing compressed data.
index
int?The starting index for decompression.
count
int?The number of bytes to decompress.
bufferSize
intThe buffer size to use during decompression.
Returns
- byte[]
A decompressed byte array.
Type Parameters
TCompressStream
The 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
input
byte[]The byte array containing the ZIP archive.
leaveOpen
boolWhether to leave the underlying stream open.
filter
Func<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
input
StreamThe stream containing the ZIP archive.
leaveOpen
boolWhether to leave the underlying stream open after processing.
filter
Func<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.