Last active
September 24, 2018 15:05
-
-
Save jgiacomini/9f7a277d15693d51fe9153875cf29ef6 to your computer and use it in GitHub Desktop.
Test Deflate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"info": { | |
"_postman_id": "82e17533-851e-446b-be19-ef7bd7a45f70", | |
"name": "deflate", | |
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" | |
}, | |
"item": [ | |
{ | |
"name": "GET_deflate", | |
"request": { | |
"method": "GET", | |
"header": [ | |
{ | |
"key": "Accept", | |
"value": "application/json" | |
} | |
], | |
"body": { | |
"mode": "raw", | |
"raw": "" | |
}, | |
"url": { | |
"raw": "http://httpbin.org/deflate", | |
"protocol": "http", | |
"host": [ | |
"httpbin", | |
"org" | |
], | |
"path": [ | |
"deflate" | |
] | |
} | |
}, | |
"response": [] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace TestDeflate | |
{ | |
public class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var value = await DeflateWithHttpClientAsync(); | |
Console.WriteLine($"{BoolToString(value)} {nameof(DeflateWithHttpClientAsync)}"); | |
value = await DeflateWithoutClientAsync(); | |
Console.WriteLine($"{BoolToString(value)} {nameof(DeflateWithoutClientAsync)}"); | |
value = await CompressAndDecompressAsync(); | |
Console.WriteLine($"{BoolToString(value)} {nameof(CompressAndDecompressAsync)}"); | |
Console.ReadLine(); | |
} | |
private static string BoolToString(bool value) | |
{ | |
return value ? "OK" : "KO"; | |
} | |
private async static Task<bool> DeflateWithHttpClientAsync() | |
{ | |
try | |
{ | |
var handler = new HttpClientHandler() | |
{ | |
AutomaticDecompression = DecompressionMethods.Deflate | |
}; | |
var httpClient = new System.Net.Http.HttpClient(handler); | |
var str = await httpClient.GetStringAsync("http://httpbin.org/deflate"); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
return false; | |
} | |
} | |
private async static Task<bool> DeflateWithoutClientAsync() | |
{ | |
try | |
{ | |
var httpClient = new System.Net.Http.HttpClient(); | |
var stream = await httpClient.GetStreamAsync("http://httpbin.org/deflate"); | |
var decompressedStream = new MemoryStream(); | |
using (var decompressionStream = new DeflateStream(stream, CompressionMode.Decompress)) | |
{ | |
await decompressionStream.CopyToAsync(decompressedStream, 81920); | |
} | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
return false; | |
} | |
} | |
private async static Task<bool> CompressAndDecompressAsync() | |
{ | |
try | |
{ | |
var info = new FileInfo("deflate.json"); | |
var unCompressedFileStream = new MemoryStream(); | |
var compressedByteArray = await CompressAsync(info.OpenRead()); | |
string compressedString = Encoding.ASCII.GetString(compressedByteArray); | |
File.WriteAllBytes("defalteCompressed.json", compressedByteArray); | |
var unCompressedByteArray = await DecompressAsync(compressedByteArray); | |
string unCompressedString = Encoding.ASCII.GetString(unCompressedByteArray); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
return false; | |
} | |
} | |
private async static Task<byte[]> CompressAsync(Stream input) | |
{ | |
using (var compressStream = new MemoryStream()) | |
{ | |
using (var compressor = new DeflateStream(compressStream, CompressionMode.Compress)) | |
{ | |
await input.CopyToAsync(compressor); | |
compressor.Close(); | |
return compressStream.ToArray(); | |
} | |
} | |
} | |
private async static Task<byte[]> DecompressAsync(byte[] data) | |
{ | |
var output = new MemoryStream(); | |
using (var compressedStream = new MemoryStream(data)) | |
using (var zipStream = new DeflateStream(compressedStream, CompressionMode.Decompress)) | |
{ | |
await zipStream.CopyToAsync(output); | |
zipStream.Close(); | |
output.Position = 0; | |
return output.ToArray(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFrameworks>net45;net46;net47;netcoreapp2.1;netcoreapp2.0</TargetFrameworks> | |
<LangVersion>latest</LangVersion> | |
</PropertyGroup> | |
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) "> | |
<Reference Include="System.Net.Http" /> | |
</ItemGroup> | |
<ItemGroup> | |
<None Update="deflate.json"> | |
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |
</None> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment