forked from NetCoreStack/WebSockets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultCompressor.cs
More file actions
43 lines (41 loc) · 1.35 KB
/
Copy pathDefaultCompressor.cs
File metadata and controls
43 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
namespace ServerTestApp
{
public class DefaultCompressor : ICompressor
{
public async Task<byte[]> CompressAsync(byte[] input)
{
using (MemoryStream memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true))
{
await gzip.WriteAsync(input, 0, input.Length);
}
return memory.ToArray();
}
}
public async Task<byte[]> DeCompressAsync(byte[] input)
{
using (GZipStream stream = new GZipStream(new MemoryStream(input), CompressionMode.Decompress))
{
byte[] buffer = new byte[1024 * 4];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = await stream.ReadAsync(buffer, 0, 1024 * 4);
if (count > 0)
{
await memory.WriteAsync(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
}
}