/// <summary> /// Gzips the stream to text. /// </summary> /// <param name="stream">The stream.</param> /// <param name="encoding">The encoding.</param> /// <returns></returns> public static string GzipStreamToText(this Stream stream, Encoding encoding) { if (stream != null) { try { using (var gZipStream = new GZipStream(stream, CompressionMode.Decompress)) { return((encoding ?? Encoding.UTF8).GetString(gZipStream.ReadStreamToBytes(true))); } } catch (Exception ex) { throw ex.Handle(new { encoding = encoding?.EncodingName }); } } return(string.Empty); }
/// <summary> /// Reads as GZip text. /// </summary> /// <param name="httpResponse">The HTTP response.</param> /// <param name="encoding">The encoding.</param> /// <returns> /// System.String. /// </returns> /// <exception cref="OperationFailureException">ReadAsGZipText</exception> private static async Task <string> InternalReadAsGZipText(this HttpResponseMessage httpResponse, Encoding encoding) { return(await httpResponse.Content.ReadAsStreamAsync().ContinueWith <string>(t => { try { using (t.Result) { using (var gZipStream = new GZipStream(t.Result, CompressionMode.Decompress)) { return (encoding ?? Encoding.UTF8).GetString(gZipStream.ReadStreamToBytes(true)); } } } catch (Exception ex) { throw ex.Handle(new { encoding = encoding?.EncodingName }); } })); }