forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetDeflateProvider.cs
More file actions
38 lines (34 loc) · 1.09 KB
/
NetDeflateProvider.cs
File metadata and controls
38 lines (34 loc) · 1.09 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
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using ServiceStack.CacheAccess;
using ServiceStack.Text;
namespace ServiceStack.Common.Support
{
public class NetDeflateProvider : IDeflateProvider
{
public byte[] Deflate(string text)
{
var buffer = Encoding.UTF8.GetBytes(text);
using(var ms = new MemoryStream())
using (var zipStream = new DeflateStream(ms, CompressionMode.Compress))
{
zipStream.Write(buffer, 0, buffer.Length);
zipStream.Close();
return ms.ToArray();
}
}
public string Inflate(byte[] gzBuffer)
{
using (var compressedStream = new MemoryStream(gzBuffer))
using (var zipStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
var utf8Bytes = zipStream.ReadFully();
return Encoding.UTF8.GetString(utf8Bytes);
}
}
}
}
#endif