private byte[] Decompress(byte[] buffer, int decompressedLength)
        {
            if (decompressedLength == 0)
            {
                return(buffer);  // Uncompressed data
            }
            // Skip header and checksum
            using (var compressed = new MemoryStream(buffer, 2, buffer.Length - 6))
            {
                var result = new byte[decompressedLength];
                using (var deflate = new DeflateStream(compressed, CompressionMode.Decompress, true))
                {
                    deflate.Read(result, 0, decompressedLength);
                }

                // Adler-32 checksum
                var checksum = BitConverter.ToUInt32(BitConverter.GetBytes(Checksum.Adler32(result)).Reverse().ToArray(), 0);
                var original = BitConverter.ToUInt32(buffer, buffer.Length - 4);
                if (checksum != original)
                {
                    throw new MyRawException("Checksum failure in compressed data stream.");
                }

                return(result);
            }
        }
        // Since DeflateStream doesn't add the zlib init header and post-data checksum, we have to do
        // some trickery.
        private byte[] Compress(byte[] buffer)
        {
            using (var compressed = new MemoryStream())
            {
                // Header
                compressed.WriteByte(0x78);
                compressed.WriteByte(0x9c);

                using (var deflate = new DeflateStream(compressed, CompressionMode.Compress, true))
                {
                    deflate.Write(buffer, 0, buffer.Length);
                }

                // Adler-32 checksum
                var checksum = Checksum.Adler32(buffer);
                foreach (var b in BitConverter.GetBytes(checksum).Reverse())
                {
                    compressed.WriteByte(b);
                }

                return(compressed.ToArray());
            }
        }