System.IO.Compression.DeflateStream.Read C# (CSharp) Method

Read() public method

public Read ( byte array, int offset, int count ) : int
array byte
offset int
count int
return int
        public override int Read(byte[] array, int offset, int count)
        {
            EnsureDecompressionMode();
            ValidateParameters(array, offset, count);
            EnsureNotDisposed();
            EnsureBufferInitialized();

            int bytesRead;
            int currentOffset = offset;
            int remainingCount = count;

            while (true)
            {
                bytesRead = _inflater.Inflate(array, currentOffset, remainingCount);
                currentOffset += bytesRead;
                remainingCount -= bytesRead;

                if (remainingCount == 0)
                {
                    break;
                }

                if (_inflater.Finished())
                {
                    // if we finished decompressing, we can't have anything left in the outputwindow.
                    Debug.Assert(_inflater.AvailableOutput == 0, "We should have copied all stuff out!");
                    break;
                }

                int bytes = _stream.Read(_buffer, 0, _buffer.Length);
                if (bytes <= 0)
                {
                    break;
                }
                else if (bytes > _buffer.Length)
                {
                    // The stream is either malicious or poorly implemented and returned a number of
                    // bytes larger than the buffer supplied to it.
                    throw new InvalidDataException(SR.GenericInvalidData);
                }

                _inflater.SetInput(_buffer, 0, bytes);
            }

            return count - remainingCount;
        }

Usage Example

        /// <summary>
        ///     The GZIP decompress.
        /// </summary>
        /// <param name="data">The data to decompress.</param>
        /// <returns>The decompressed data</returns>
        public static byte[] DeflateDecompress(this byte[] data)
        {
            byte[] bytes = null;
            if (data != null)
            {
                using (var input = new MemoryStream(data.Length))
                {
                    input.Write(data, 0, data.Length);
                    input.Position = 0;

                    var gzip = new DeflateStream(input, CompressionMode.Decompress);

                    using (var output = new MemoryStream(data.Length))
                    {
                        var buff = new byte[64];
                        int read = gzip.Read(buff, 0, buff.Length);

                        while (read > 0)
                        {
                            output.Write(buff, 0, buff.Length);
                            read = gzip.Read(buff, 0, buff.Length);
                        }

                        bytes = output.ToArray();
                    }
                }
            }

            return bytes;
        }
All Usage Examples Of System.IO.Compression.DeflateStream::Read