本文整理汇总了C#中System.IO.Compression.DeflateStream.ReadStream方法的典型用法代码示例。如果您正苦于以下问题:C# DeflateStream.ReadStream方法的具体用法?C# DeflateStream.ReadStream怎么用?C# DeflateStream.ReadStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Compression.DeflateStream
的用法示例。
在下文中一共展示了DeflateStream.ReadStream方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decompress
/// <summary>
/// Decompress a byte array.
/// </summary>
/// <param name="source">The <see cref="Byte"/> array to decompress.</param>
/// <param name="length">The number of bytes to read into the byte array for compression.</param>
/// <param name="startIndex">An <see cref="Int32"/> representing the start index of the byte array.</param>
/// <returns>A decompressed <see cref="Byte"/> array.</returns>
public static byte[] Decompress(this byte[] source, int startIndex, int length)
{
const byte CompressionStrengthMask = (byte)(Bits.Bit00 | Bits.Bit01);
// Unmask compression strength from first buffer byte
CompressionStrength strength = (CompressionStrength)(source[startIndex] & CompressionStrengthMask);
if (strength == CompressionStrength.NoCompression)
{
// No compression was applied to original buffer, return specified portion of the buffer
return source.BlockCopy(startIndex + 1, length - 1);
}
else
{
// Create a new decompression deflater
MemoryStream compressedData = new MemoryStream(source, startIndex + 1, length - 1);
DeflateStream inflater = new DeflateStream(compressedData, CompressionMode.Decompress);
int compressionDepth = (source[startIndex] & ~CompressionStrengthMask) >> 2;
// Read uncompressed data
byte[] destination = inflater.ReadStream();
// When user requests muli-pass compression, there may be multiple compression passes on a buffer,
// so we cycle through the needed uncompressions to get back to the original data
if (strength == CompressionStrength.MultiPass && compressionDepth > 0)
return destination.Decompress();
else
return destination;
}
}