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
57 lines (50 loc) · 1.75 KB
/
NetDeflateProvider.cs
File metadata and controls
57 lines (50 loc) · 1.75 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#if !(SL5 || XBOX || ANDROID || __IOS__ || __MAC__ || PCL)
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)
{
return Deflate(Encoding.UTF8.GetBytes(text));
}
public byte[] Deflate(byte[] bytes)
{
// 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(bytes, 0, bytes.Length);
zipStream.Close();
return ms.ToArray();
}
}
public string Inflate(byte[] gzBuffer)
{
var utf8Bytes = InflateBytes(gzBuffer);
return Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
}
public byte[] InflateBytes(byte[] gzBuffer)
{
using (var compressedStream = new MemoryStream(gzBuffer))
using (var zipStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
return zipStream.ReadFully();
}
}
public Stream DeflateStream(Stream outputStream)
{
return new DeflateStream(outputStream, CompressionMode.Compress);
}
public Stream InflateStream(Stream inputStream)
{
return new DeflateStream(inputStream, CompressionMode.Decompress);
}
}
}
#endif