|
6 | 6 | using System.IO; |
7 | 7 | using System.Text; |
8 | 8 |
|
9 | | -namespace ServiceStack |
| 9 | +namespace ServiceStack; |
| 10 | + |
| 11 | +public static class StreamExt |
10 | 12 | { |
11 | | - public static class StreamExt |
| 13 | + /// <summary> |
| 14 | + /// Compresses the specified text using the default compression method: Deflate |
| 15 | + /// </summary> |
| 16 | + /// <param name="text">The text.</param> |
| 17 | + /// <param name="compressionType">Type of the compression.</param> |
| 18 | + /// <param name="encoding"></param> |
| 19 | + /// <returns></returns> |
| 20 | + public static byte[] Compress(this string text, string compressionType, Encoding? encoding=null) => |
| 21 | + StreamCompressors.GetRequired(compressionType).Compress(text, encoding); |
| 22 | + |
| 23 | + public static Stream CompressStream(this Stream stream, string compressionType) => |
| 24 | + StreamCompressors.GetRequired(compressionType).Compress(stream); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Compresses the specified text using the default compression method: Deflate |
| 28 | + /// </summary> |
| 29 | + public static byte[] CompressBytes(this byte[] bytes, string compressionType) => |
| 30 | + StreamCompressors.GetRequired(compressionType).Compress(bytes); |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Decompresses the specified gz buffer using the default compression method: Inflate |
| 34 | + /// </summary> |
| 35 | + /// <param name="gzBuffer">The gz buffer.</param> |
| 36 | + /// <param name="compressionType">Type of the compression.</param> |
| 37 | + /// <returns></returns> |
| 38 | + public static string Decompress(this byte[] gzBuffer, string compressionType) => |
| 39 | + StreamCompressors.GetRequired(compressionType).Decompress(gzBuffer); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Decompresses the specified gz buffer using inflate or gzip method |
| 43 | + /// </summary> |
| 44 | + /// <param name="gzStream">Compressed stream</param> |
| 45 | + /// <param name="compressionType">Type of the compression. Can be "gzip" or "deflate"</param> |
| 46 | + /// <returns>Decompressed stream</returns> |
| 47 | + public static Stream Decompress(this Stream gzStream, string compressionType) => |
| 48 | + !string.IsNullOrEmpty(compressionType) |
| 49 | + ? StreamCompressors.GetRequired(compressionType).Decompress(gzStream) |
| 50 | + : gzStream; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Decompresses the specified gz buffer using the default compression method: Inflate |
| 54 | + /// </summary> |
| 55 | + public static byte[] DecompressBytes(this byte[] gzBuffer, string compressionType) => |
| 56 | + StreamCompressors.GetRequired(compressionType).DecompressBytes(gzBuffer); |
| 57 | + |
| 58 | + public static byte[] Deflate(this string text) => DeflateCompressor.Instance.Compress(text); |
| 59 | + |
| 60 | + public static string Inflate(this byte[] gzBuffer) => DeflateCompressor.Instance.Decompress(gzBuffer); |
| 61 | + |
| 62 | + public static byte[] GZip(this string text) => GZipCompressor.Instance.Compress(text); |
| 63 | + |
| 64 | + public static string GUnzip(this byte[] gzBuffer) => GZipCompressor.Instance.Decompress(gzBuffer); |
| 65 | + |
| 66 | + public static string ToUtf8String(this Stream stream) |
12 | 67 | { |
13 | | - /// <summary> |
14 | | - /// Compresses the specified text using the default compression method: Deflate |
15 | | - /// </summary> |
16 | | - /// <param name="text">The text.</param> |
17 | | - /// <param name="compressionType">Type of the compression.</param> |
18 | | - /// <param name="encoding"></param> |
19 | | - /// <returns></returns> |
20 | | - public static byte[] Compress(this string text, string compressionType, Encoding? encoding=null) => |
21 | | - StreamCompressors.GetRequired(compressionType).Compress(text, encoding); |
22 | | - |
23 | | - public static Stream CompressStream(this Stream stream, string compressionType) => |
24 | | - StreamCompressors.GetRequired(compressionType).Compress(stream); |
25 | | - |
26 | | - /// <summary> |
27 | | - /// Compresses the specified text using the default compression method: Deflate |
28 | | - /// </summary> |
29 | | - public static byte[] CompressBytes(this byte[] bytes, string compressionType) => |
30 | | - StreamCompressors.GetRequired(compressionType).Compress(bytes); |
31 | | - |
32 | | - /// <summary> |
33 | | - /// Decompresses the specified gz buffer using the default compression method: Inflate |
34 | | - /// </summary> |
35 | | - /// <param name="gzBuffer">The gz buffer.</param> |
36 | | - /// <param name="compressionType">Type of the compression.</param> |
37 | | - /// <returns></returns> |
38 | | - public static string Decompress(this byte[] gzBuffer, string compressionType) => |
39 | | - StreamCompressors.GetRequired(compressionType).Decompress(gzBuffer); |
40 | | - |
41 | | - /// <summary> |
42 | | - /// Decompresses the specified gz buffer using inflate or gzip method |
43 | | - /// </summary> |
44 | | - /// <param name="gzStream">Compressed stream</param> |
45 | | - /// <param name="compressionType">Type of the compression. Can be "gzip" or "deflate"</param> |
46 | | - /// <returns>Decompressed stream</returns> |
47 | | - public static Stream Decompress(this Stream gzStream, string compressionType) => |
48 | | - !string.IsNullOrEmpty(compressionType) |
49 | | - ? StreamCompressors.GetRequired(compressionType).Decompress(gzStream) |
50 | | - : gzStream; |
51 | | - |
52 | | - /// <summary> |
53 | | - /// Decompresses the specified gz buffer using the default compression method: Inflate |
54 | | - /// </summary> |
55 | | - public static byte[] DecompressBytes(this byte[] gzBuffer, string compressionType) => |
56 | | - StreamCompressors.GetRequired(compressionType).DecompressBytes(gzBuffer); |
57 | | - |
58 | | - public static byte[] Deflate(this string text) => DeflateCompressor.Instance.Compress(text); |
59 | | - |
60 | | - public static string Inflate(this byte[] gzBuffer) => DeflateCompressor.Instance.Decompress(gzBuffer); |
61 | | - |
62 | | - public static byte[] GZip(this string text) => GZipCompressor.Instance.Compress(text); |
63 | | - |
64 | | - public static string GUnzip(this byte[] gzBuffer) => GZipCompressor.Instance.Decompress(gzBuffer); |
65 | | - |
66 | | - public static string ToUtf8String(this Stream stream) |
67 | | - { |
68 | | - if (stream == null) |
69 | | - throw new ArgumentNullException(nameof(stream)); |
70 | | - |
71 | | - return stream.ReadToEnd(); |
72 | | - } |
73 | | - |
74 | | - public static byte[] ToBytes(this Stream stream) |
75 | | - { |
76 | | - if (stream == null) |
77 | | - throw new ArgumentNullException(nameof(stream)); |
78 | | - |
79 | | - return stream.ReadFully(); |
80 | | - } |
81 | | - |
82 | | - public static void Write(this Stream stream, string text) |
83 | | - { |
84 | | - var bytes = Encoding.UTF8.GetBytes(text); |
85 | | - stream.Write(bytes, 0, bytes.Length); |
86 | | - } |
| 68 | + if (stream == null) |
| 69 | + throw new ArgumentNullException(nameof(stream)); |
| 70 | + |
| 71 | + return stream.ReadToEnd(); |
87 | 72 | } |
| 73 | + |
| 74 | + public static byte[] ToBytes(this Stream stream) |
| 75 | + { |
| 76 | + if (stream == null) |
| 77 | + throw new ArgumentNullException(nameof(stream)); |
| 78 | + |
| 79 | + return stream.ReadFully(); |
| 80 | + } |
| 81 | + |
| 82 | + public static void Write(this Stream stream, string text) |
| 83 | + { |
| 84 | + var bytes = Encoding.UTF8.GetBytes(text); |
| 85 | + stream.Write(bytes, 0, bytes.Length); |
| 86 | + } |
| 87 | + |
| 88 | + public static string ComputeSha256(this byte[] data) |
| 89 | + { |
| 90 | + using var sha256 = System.Security.Cryptography.SHA256.Create(); |
| 91 | + byte[] hashBytes = sha256.ComputeHash(data); |
| 92 | + return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); |
| 93 | + } |
| 94 | + |
| 95 | + public static string ComputeSha256(this Stream stream) |
| 96 | + { |
| 97 | + stream.Position = 0; |
| 98 | + using var sha256 = System.Security.Cryptography.SHA256.Create(); |
| 99 | + byte[] hashBytes = sha256.ComputeHash(stream); |
| 100 | + return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); |
| 101 | + } |
88 | 102 | } |
0 commit comments