Пример #1
0
 /// <summary>
 /// Uncompress a byte array into a byte array.
 /// </summary>
 /// <seealso cref="ZlibStream.CompressBuffer(byte[])"/>
 /// <seealso cref="ZlibStream.UncompressString(byte[])"/>
 /// <param name="compressed">
 /// A buffer containing ZLIB-compressed data.
 /// </param>
 public static byte[] UncompressBuffer(byte[] compressed)
 {
     // workitem 8460
     byte[] working = new byte[1024];
     using (var output = new MemoryStream())
     {
         using (var input = new MemoryStream(compressed))
         {
             using (Stream decompressor = new ZlibStream(input))
             {
                 int n;
                 while ((n = decompressor.Read(working, 0, working.Length)) != 0)
                 {
                     output.Write(working, 0, n);
                 }
             }
             return(output.ToArray());
         }
     }
 }
Пример #2
0
		/// <summary>
		/// Uncompress a byte array into a byte array.
		/// </summary>
		/// <seealso cref="ZlibStream.CompressBuffer(byte[])"/>
		/// <seealso cref="ZlibStream.UncompressString(byte[])"/>
		/// <param name="compressed">
		/// A buffer containing ZLIB-compressed data.  
		/// </param>
		public static byte[] UncompressBuffer(byte[] compressed)
		{
			// workitem 8460
			byte[] working = new byte[1024];
			using (var output = new MemoryStream())
			{
				using (var input = new MemoryStream(compressed))
				{
					using (Stream decompressor = new ZlibStream(input))
					{
						int n;
						while ((n = decompressor.Read(working, 0, working.Length)) != 0)
						{
							output.Write(working, 0, n);
						}
					}
					return output.ToArray();
				}
			}
		}
Пример #3
0
		/// <summary>
		/// Uncompress a byte array into a single string.
		/// </summary>
		/// <seealso cref="ZlibStream.CompressString(String)"/>
		/// <param name="compressed">
		/// A buffer containing ZLIB-compressed data.  
		/// </param>
		public static String UncompressString(byte[] compressed)
		{
			// workitem 8460
			byte[] working = new byte[1024];
			var encoding = System.Text.Encoding.UTF8;
			using (var output = new MemoryStream())
			{
				using (var input = new MemoryStream(compressed))
				{
					using (Stream decompressor = new ZlibStream(input))
					{
						int n;
						while ((n = decompressor.Read(working, 0, working.Length)) != 0)
						{
							output.Write(working, 0, n);
						}
					}
					// reset to allow read from start
					output.Seek(0, SeekOrigin.Begin);
					var sr = new StreamReader(output, encoding);
					return sr.ReadToEnd();
				}
			}
		}
Пример #4
0
        private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
        {
            using (webResponse)
            {
#if FRAMEWORK
                response.ContentEncoding = webResponse.ContentEncoding;
                response.Server = webResponse.Server;
#endif
                response.ContentType = webResponse.ContentType;
                response.ContentLength = webResponse.ContentLength;

                Stream webResponseStream = webResponse.GetResponseStream();

#if WINDOWS_PHONE || UNITY
                string contentEncoding = "";
                if(webResponse.Headers[HttpResponseHeader.ContentEncoding] != null) { 
                    contentEncoding = webResponse.Headers[HttpResponseHeader.ContentEncoding];
                }
                    
                if (string.Equals(contentEncoding, "gzip", StringComparison.OrdinalIgnoreCase))
                {
                    GZipStream gzStream = new GZipStream(webResponseStream);

                    ProcessResponseStream(gzStream, response);
                }
                else if (string.Equals(contentEncoding, "deflate", StringComparison.OrdinalIgnoreCase))
                {
                    ZlibStream dfStream = new ZlibStream(webResponseStream);
                    
                    ProcessResponseStream(dfStream, response);
                }
                else
                { 
                    ProcessResponseStream(webResponseStream, response);
                }
#else
                this.ProcessResponseStream(webResponseStream, response);
#endif

                response.StatusCode = webResponse.StatusCode;
                response.StatusDescription = webResponse.StatusDescription;
                response.ResponseUri = webResponse.ResponseUri;
                response.ResponseStatus = ResponseStatus.Completed;

                if (webResponse.Cookies != null)
                {
                    foreach (Cookie cookie in webResponse.Cookies)
                    {
                        response.Cookies.Add(new HttpCookie
                                             {
                                                 Comment = cookie.Comment,
                                                 CommentUri = cookie.CommentUri,
                                                 Discard = cookie.Discard,
                                                 Domain = cookie.Domain,
                                                 Expired = cookie.Expired,
                                                 Expires = cookie.Expires,
                                                 HttpOnly = cookie.HttpOnly,
                                                 Name = cookie.Name,
                                                 Path = cookie.Path,
                                                 Port = cookie.Port,
                                                 Secure = cookie.Secure,
                                                 TimeStamp = cookie.TimeStamp,
                                                 Value = cookie.Value,
                                                 Version = cookie.Version
                                             });
                    }
                }

                foreach (string headerName in webResponse.Headers.AllKeys)
                {
                    string headerValue = webResponse.Headers[headerName];

                    response.Headers.Add(new HttpHeader
                                         {
                                             Name = headerName,
                                             Value = headerValue
                                         });
                }

                webResponse.Close();
            }
        }