forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamExtensions.cs
More file actions
69 lines (56 loc) · 1.87 KB
/
StreamExtensions.cs
File metadata and controls
69 lines (56 loc) · 1.87 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
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using ServiceStack.CacheAccess;
using ServiceStack.Common.Support;
using ServiceStack.Common.Web;
namespace ServiceStack.Common
{
public static class StreamExtensions
{
/// <summary>
/// Compresses the specified text using the default compression method: Deflate
/// </summary>
/// <param name="text">The text.</param>
/// <param name="compressionType">Type of the compression.</param>
/// <returns></returns>
public static byte[] Compress(this string text, string compressionType)
{
if (compressionType == CompressionTypes.Deflate)
return Deflate(text);
if (compressionType == CompressionTypes.GZip)
return GZip(text);
throw new NotSupportedException(compressionType);
}
public static IDeflateProvider DeflateProvider = new NetDeflateProvider();
public static IGZipProvider GZipProvider = new NetGZipProvider();
/// <summary>
/// Decompresses the specified gz buffer using the default compression method: Inflate
/// </summary>
/// <param name="gzBuffer">The gz buffer.</param>
/// <param name="compressionType">Type of the compression.</param>
/// <returns></returns>
public static string Decompress(this byte[] gzBuffer, string compressionType)
{
if (compressionType == CompressionTypes.Deflate)
return Inflate(gzBuffer);
if (compressionType == CompressionTypes.GZip)
return GUnzip(gzBuffer);
throw new NotSupportedException(compressionType);
}
public static byte[] Deflate(this string text)
{
return DeflateProvider.Deflate(text);
}
public static string Inflate(this byte[] gzBuffer)
{
return DeflateProvider.Inflate(gzBuffer);
}
public static byte[] GZip(this string text)
{
return GZipProvider.GZip(text);
}
public static string GUnzip(this byte[] gzBuffer)
{
return GZipProvider.GUnzip(gzBuffer);
}
}
}