CompressionHelper

Ecng.IO.Compression

Provides helper methods for compressing and decompressing data using various algorithms.

Methods

CompressAsync``1
public static Task<byte[]> CompressAsync<TCompressStream>(byte[] input, int? index, int? count, CompressionLevel level, int bufferSize, CancellationToken cancellationToken)
result = CompressionHelper.CompressAsync(input, index, count, level, bufferSize, cancellationToken)

Asynchronously compresses a portion of a byte array using the specified compression stream.

input
The byte array to compress.
index
The starting index for compression.
count
The number of bytes to compress.
level
The compression level to use.
bufferSize
The buffer size to use during compression.
cancellationToken
A token to monitor for cancellation requests.

Returns: A task representing the asynchronous operation, with a compressed byte array as the result.

CompressAsync``1
public static Task CompressAsync<TCompressStream>(Stream input, Stream output, CompressionLevel level, bool leaveOpen, int bufferSize, CancellationToken cancellationToken)
result = CompressionHelper.CompressAsync(input, output, level, leaveOpen, bufferSize, cancellationToken)

Asynchronously compresses data from the input stream and writes the compressed data to the output stream using the specified compression stream.

input
The input stream containing data to compress.
output
The output stream to write compressed data to.
level
The compression level to use.
leaveOpen
Whether to leave the output stream open after compression.
bufferSize
The buffer size to use during compression.
cancellationToken
A token to monitor for cancellation requests.

Returns: A task representing the asynchronous compression operation.

CompressAsync``1
public static Task<byte[]> CompressAsync<TCompressStream>(ReadOnlyMemory<byte> input, CompressionLevel level, int bufferSize, CancellationToken cancellationToken)
result = CompressionHelper.CompressAsync(input, level, bufferSize, cancellationToken)

Asynchronously compresses a read-only memory of bytes using the specified compression stream.

input
The read-only memory containing data to compress.
level
The compression level to use.
bufferSize
The buffer size to use during compression.
cancellationToken
A token to monitor for cancellation requests.

Returns: A task representing the asynchronous operation, with a compressed byte array as the result.

Compress``1
public static byte[] Compress<TCompressStream>(byte[] input, int? index, int? count, CompressionLevel level, int bufferSize)
result = CompressionHelper.Compress(input, index, count, level, bufferSize)

Compresses a portion of a byte array using the specified compression stream.

input
The byte array to compress.
index
The starting index for compression.
count
The number of bytes to compress.
level
The compression level to use.
bufferSize
The buffer size to use during compression.

Returns: A compressed byte array.

DeflateFrom
public static byte[] DeflateFrom(byte[] input, int? index, int? count, int bufferSize)
result = CompressionHelper.DeflateFrom(input, index, count, bufferSize)

Decompresses a portion of a byte array using the Deflate algorithm.

input
The byte array to decompress.
index
The starting index from which to begin decompression.
count
The number of bytes to decompress.
bufferSize
The buffer size to use during decompression.

Returns: A decompressed byte array.

DeflateFrom
public static byte[] DeflateFrom(ReadOnlySpan<byte> input, int bufferSize)
result = CompressionHelper.DeflateFrom(input, bufferSize)

Decompresses a span of bytes using the Deflate algorithm.

input
The span to decompress.
bufferSize
The buffer size to use during decompression.

Returns: A decompressed byte array.

DeflateTo
public static byte[] DeflateTo(byte[] input)
result = CompressionHelper.DeflateTo(input)

Compresses the specified byte array using the Deflate algorithm.

input
The byte array to compress.

Returns: A compressed byte array.

Do7Zip
public static byte[] Do7Zip(byte[] input)
result = CompressionHelper.Do7Zip(input)

Compresses the specified byte array using the 7Zip (LZMA) algorithm.

input
The byte array to compress.

Returns: A compressed byte array with LZMA header (5 bytes properties + 8 bytes size + compressed data).

GZipAsync
public static Task<byte[]> GZipAsync(byte[] data, CancellationToken cancellationToken)
result = CompressionHelper.GZipAsync(data, cancellationToken)

Compresses the data with GZip.

data
The data to compress.
cancellationToken
A token to monitor for cancellation requests.

Returns: The GZip-compressed bytes.

Un7Zip
public static byte[] Un7Zip(byte[] input)
result = CompressionHelper.Un7Zip(input)

Decompresses a 7Zip (LZMA) compressed byte array.

input
The byte array containing 7Zip-compressed data with LZMA header.

Returns: A decompressed byte array.

UncompressAsync``1
public static Task UncompressAsync<TCompressStream>(Stream input, Stream output, bool leaveOpen, int bufferSize, CancellationToken cancellationToken)
result = CompressionHelper.UncompressAsync(input, output, leaveOpen, bufferSize, cancellationToken)

Asynchronously decompresses data from the input stream and writes the decompressed data to the output stream using the specified compression stream.

input
The input stream containing compressed data.
output
The output stream to write decompressed data to.
leaveOpen
Whether to leave the input stream open after decompression.
bufferSize
The buffer size to use during decompression.
cancellationToken
A token to monitor for cancellation requests.

Returns: A task representing the asynchronous decompression operation.

UncompressAsync``1
public static Task<byte[]> UncompressAsync<TCompressStream>(byte[] input, int? index, int? count, int bufferSize, CancellationToken cancellationToken)
result = CompressionHelper.UncompressAsync(input, index, count, bufferSize, cancellationToken)

Asynchronously decompresses a portion of a byte array using the specified compression stream.

input
The byte array containing compressed data.
index
The starting index for decompression.
count
The number of bytes to decompress.
bufferSize
The buffer size to use during decompression.
cancellationToken
A token to monitor for cancellation requests.

Returns: A task representing the asynchronous decompression operation, with a decompressed byte array as the result.

Uncompress``1
public static byte[] Uncompress<TCompressStream>(byte[] input, int? index, int? count, int bufferSize)
result = CompressionHelper.Uncompress(input, index, count, bufferSize)

Decompresses a portion of a byte array using the specified compression stream.

input
The byte array containing compressed data.
index
The starting index for decompression.
count
The number of bytes to decompress.
bufferSize
The buffer size to use during decompression.

Returns: A decompressed byte array.

UnDeflate
public static string UnDeflate(ReadOnlySpan<byte> input)
result = CompressionHelper.UnDeflate(input)

Decompresses a Deflate-compressed span of bytes into a UTF-8 encoded string.

input
The span containing Deflate-compressed data.

Returns: A string resulting from decompression.

UnDeflate
public static string UnDeflate(byte[] input, int index, int count)
result = CompressionHelper.UnDeflate(input, index, count)

Decompresses a portion of a Deflate-compressed byte array into a UTF-8 encoded string.

input
The byte array containing Deflate-compressed data.
index
The starting index for decompression.
count
The number of bytes to decompress.

Returns: A string resulting from decompression.

UnDeflate
public static string UnDeflate(byte[] input)
result = CompressionHelper.UnDeflate(input)

Decompresses a Deflate-compressed byte array into a UTF-8 encoded string.

input
The byte array containing Deflate-compressed data.

Returns: A string resulting from decompression.

UnDeflate
public static int UnDeflate(ReadOnlySpan<byte> input, Span<byte> destination)
result = CompressionHelper.UnDeflate(input, destination)

Decompresses a Deflate-compressed span of bytes into the provided destination span.

input
The span containing Deflate-compressed data.
destination
The span to store decompressed data.

Returns: The number of bytes written into the destination span.

UnDeflate
public static int UnDeflate(byte[] input, int index, int count, byte[] destination)
result = CompressionHelper.UnDeflate(input, index, count, destination)

Decompresses a portion of a Deflate-compressed byte array into the provided destination buffer.

input
The byte array containing Deflate-compressed data.
index
The starting index for decompression.
count
The number of bytes to decompress.
destination
The buffer to store decompressed data.

Returns: The number of bytes written into the destination buffer.

UnGZip
public static int UnGZip(byte[] input, int index, int count, byte[] destination)
result = CompressionHelper.UnGZip(input, index, count, destination)

Decompresses a portion of a GZip-compressed byte array into the provided destination buffer.

input
The byte array containing GZip-compressed data.
index
The starting index for decompression.
count
The number of bytes to decompress.
destination
The buffer to store decompressed data.

Returns: The number of bytes written into the destination buffer.

UnGZip
public static int UnGZip(ReadOnlySpan<byte> input, Span<byte> destination)
result = CompressionHelper.UnGZip(input, destination)

Decompresses a GZip-compressed span of bytes into the provided destination span.

input
The span containing GZip-compressed data.
destination
The span to store decompressed data.

Returns: The number of bytes written into the destination span.

UnGZip
public static string UnGZip(byte[] input, int index, int count)
result = CompressionHelper.UnGZip(input, index, count)

Decompresses a GZip-compressed portion of a byte array into a UTF-8 encoded string.

input
The byte array containing GZip-compressed data.
index
The starting index from which to begin decompression.
count
The number of bytes to decompress.

Returns: A string resulting from decompression.

UnGZip
public static string UnGZip(ReadOnlySpan<byte> input)
result = CompressionHelper.UnGZip(input)

Decompresses a GZip-compressed span of bytes into a UTF-8 encoded string.

input
The span containing GZip-compressed data.

Returns: A string resulting from decompression.

UnGZip
public static string UnGZip(byte[] input)
result = CompressionHelper.UnGZip(input)

Decompresses a GZip-compressed byte array into a UTF-8 encoded string.

input
The byte array containing GZip-compressed data.

Returns: A string resulting from decompression.

UnGZipAsync
public static Task<byte[]> UnGZipAsync(byte[] data, CancellationToken cancellationToken)
result = CompressionHelper.UnGZipAsync(data, cancellationToken)

Decompresses GZip-compressed data to bytes.

data
The GZip-compressed data.
cancellationToken
A token to monitor for cancellation requests.

Returns: The decompressed bytes.

Unzip
public static IEnumerable<ValueTuple<string, Stream>> Unzip(byte[] input, Func<string, bool> filter)
result = CompressionHelper.Unzip(input, filter)

Extracts entries from a ZIP archive contained in the specified byte array.

input
The byte array containing the ZIP archive.
filter
A function to filter entries by name. Only entries returning true will be processed.

Returns: An enumerable of entries. The enumerator must be disposed after use (foreach does this automatically).

Unzip
public static IEnumerable<ValueTuple<string, Stream>> Unzip(Stream input, bool leaveOpen, Func<string, bool> filter)
result = CompressionHelper.Unzip(input, leaveOpen, filter)

Extracts entries from a ZIP archive contained in the specified stream.

input
The stream containing the ZIP archive.
leaveOpen
Whether to leave the underlying stream open after processing.
filter
A function to filter entries by name. Only entries returning true will be processed.

Returns: An enumerable of entries. The enumerator must be disposed after use (foreach does this automatically).

Zip
public static void Zip(IEnumerable<ValueTuple<string, Stream>> entries, Stream output, CompressionLevel level, bool leaveOpen)
CompressionHelper.Zip(entries, output, level, leaveOpen)

Creates a ZIP archive from a collection of entries and writes it to the specified stream.

entries
The entries to include in the archive. Each entry is a tuple of (name, stream).
output
The output stream to write the ZIP archive to.
level
The compression level to use.
leaveOpen
Whether to leave the output stream open after creating the archive.
ZipAsync
public static Task ZipAsync(IEnumerable<ValueTuple<string, Stream>> entries, Stream output, CompressionLevel level, bool leaveOpen, CancellationToken cancellationToken)
result = CompressionHelper.ZipAsync(entries, output, level, leaveOpen, cancellationToken)

Creates a ZIP archive from a collection of entries and writes it to the specified stream asynchronously.

entries
The entries to include in the archive. Each entry is a tuple of (name, stream).
output
The output stream to write the ZIP archive to.
level
The compression level to use.
leaveOpen
Whether to leave the output stream open after creating the archive.
cancellationToken
A token to monitor for cancellation requests.

Fields

DefaultBufferSize
public const int DefaultBufferSize
value = CompressionHelper.DefaultBufferSize

Represents the default buffer size used in compression and decompression operations.