Пример #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="contentEncoding"></param>
        /// <param name="rawRead"></param>
        /// <param name="bufferSize"></param>
        /// <returns></returns>
        public static byte[] DeCompressContent(byte[] content, string contentEncoding, byte[] rawRead, int bufferSize)
        {
            using (MemoryStream inputStream = new MemoryStream(content))
            {
                // read in and convert gzipped content compressed content and then fix it up
                using (CompressedStream deCompressStream = new CompressedStream(contentEncoding, inputStream, CompressionMode.Decompress))
                {
                    using (MemoryStream outputSteam = new MemoryStream())
                    {
                        int bytes;
                        do
                        {
                            bytes = deCompressStream.Read(rawRead, 0, bufferSize);
                            if (bytes != 0)
                                outputSteam.Write(rawRead, 0, bytes);
                        }
                        while (bytes != 0);
                        outputSteam.Flush();

                        return outputSteam.ToArray();
                    }
                }
            }
        }
Пример #2
0
        private byte[] ProcessCompressedContent(string match, string replace)
        {
            try
            {
                // deal with gzipped data
                byte[] rawContent = CompressedStream.DeCompressContent(_content.ToArray(), _contentEncoding, _rawRead, BufferSize);

                // do Tranform on decompressed data
                rawContent = Replace(new List<byte>(rawContent), match, replace).ToArray();
                rawContent = _messageHandler.ProcessResponseContent(this, rawContent);

                // we have fixed up and decompressed the gzipped data, now gzip (compress) it back up
                using (MemoryStream rewriteStream = new MemoryStream())
                {
                    using (CompressedStream compressStream = new CompressedStream(_contentEncoding, rewriteStream, CompressionMode.Compress))
                    {
                        compressStream.Write(rawContent, 0, rawContent.Length);
                        compressStream.Flush();
                        rewriteStream.Flush();
                    }
                    return rewriteStream.ToArray();
                }
            }
            catch (Exception excp)
            {
                LogFactory.LogException(excp, "compression");
                // try to return just the raw data
                return _content.ToArray();
            }
        }