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
39 lines (35 loc) · 1.26 KB
/
NetDeflateProvider.cs
File metadata and controls
39 lines (35 loc) · 1.26 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
#if !(SL5 || XBOX || ANDROID || __IOS__)
using System.IO;
using System.IO.Compression;
using System.Text;
using ServiceStack.Caching;
using ServiceStack.Text;
namespace ServiceStack.Support
{
public class NetDeflateProvider : IDeflateProvider
{
public byte[] Deflate(string text)
{
var buffer = Encoding.UTF8.GetBytes(text);
// In .NET FX incompat-ville, you can't access compressed bytes without closing DeflateStream
// Which means we must use MemoryStream since you have to use ToArray() on a closed Stream
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, 0, utf8Bytes.Length);
}
}
}
}
#endif