|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.IO; |
4 |
| -using System.IO.Compression; |
5 |
| -using System.Linq; |
6 |
| -using System.Runtime.Serialization.Formatters.Binary; |
7 |
| -using System.Text; |
8 |
| -using System.Threading.Tasks; |
9 |
| - |
10 |
| -namespace Myvas.AspNetCore.Authentication.WeixinAuth.Internal |
11 |
| -{ |
12 |
| - /// <summary> |
13 |
| - /// ref. https://stackoverflow.com/questions/7343465/compression-decompression-string-with-c-sharp |
14 |
| - /// </summary> |
15 |
| - static internal class CompressionExtensions |
16 |
| - { |
17 |
| - public static async Task<IEnumerable<byte>> Zip(this object obj) |
18 |
| - { |
19 |
| - byte[] bytes = obj.Serialize(); |
20 |
| - |
21 |
| - using (MemoryStream msi = new MemoryStream(bytes)) |
22 |
| - using (MemoryStream mso = new MemoryStream()) |
23 |
| - { |
24 |
| - using (var gs = new GZipStream(mso, CompressionMode.Compress)) |
25 |
| - await msi.CopyToAsync(gs); |
26 |
| - |
27 |
| - return mso.ToArray().AsEnumerable(); |
28 |
| - } |
29 |
| - } |
30 |
| - |
31 |
| - public static async Task<object> Unzip(this byte[] bytes) |
32 |
| - { |
33 |
| - using (MemoryStream msi = new MemoryStream(bytes)) |
34 |
| - using (MemoryStream mso = new MemoryStream()) |
35 |
| - { |
36 |
| - using (var gs = new GZipStream(msi, CompressionMode.Decompress)) |
37 |
| - { |
38 |
| - //gs.CopyTo(mso); |
39 |
| - await gs.CopyToAsync(mso); |
40 |
| - } |
41 |
| - |
42 |
| - return mso.ToArray().Deserialize(); |
43 |
| - } |
44 |
| - } |
45 |
| - } |
46 |
| - |
47 |
| - internal static class SerializerExtensions |
48 |
| - { |
49 |
| - /// <summary> |
50 |
| - /// Writes the given object instance to a binary file. |
51 |
| - /// <para>Object type (and all child types) must be decorated with the [Serializable] attribute.</para> |
52 |
| - /// <para>To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties.</para> |
53 |
| - /// </summary> |
54 |
| - /// <typeparam name="T">The type of object being written to the XML file.</typeparam> |
55 |
| - /// <param name="filePath">The file path to write the object instance to.</param> |
56 |
| - /// <param name="objectToWrite">The object instance to write to the XML file.</param> |
57 |
| - /// <param name="append">If false the file will be overwritten if it already exists. If true the contents will be appended to the file.</param> |
58 |
| - public static byte[] Serialize<T>(this T objectToWrite) |
59 |
| - { |
60 |
| - using (MemoryStream stream = new MemoryStream()) |
61 |
| - { |
62 |
| - BinaryFormatter binaryFormatter = new BinaryFormatter(); |
63 |
| - binaryFormatter.Serialize(stream, objectToWrite); |
64 |
| - |
65 |
| - return stream.GetBuffer(); |
66 |
| - } |
67 |
| - } |
68 |
| - |
69 |
| - /// <summary> |
70 |
| - /// Reads an object instance from a binary file. |
71 |
| - /// </summary> |
72 |
| - /// <typeparam name="T">The type of object to read from the XML.</typeparam> |
73 |
| - /// <param name="filePath">The file path to read the object instance from.</param> |
74 |
| - /// <returns>Returns a new instance of the object read from the binary file.</returns> |
75 |
| - public static async Task<T> _Deserialize<T>(this byte[] arr) |
76 |
| - { |
77 |
| - using (MemoryStream stream = new MemoryStream()) |
78 |
| - { |
79 |
| - BinaryFormatter binaryFormatter = new BinaryFormatter(); |
80 |
| - await stream.WriteAsync(arr, 0, arr.Length); |
81 |
| - stream.Position = 0; |
82 |
| - |
83 |
| - return (T)binaryFormatter.Deserialize(stream); |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - public static async Task<object> Deserialize(this byte[] arr) |
88 |
| - { |
89 |
| - object obj = await arr._Deserialize<object>(); |
90 |
| - return obj; |
91 |
| - } |
92 |
| - } |
93 |
| -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.IO.Compression; |
| 5 | +using System.Linq; |
| 6 | +using System.Runtime.Serialization.Formatters.Binary; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Xml.Serialization; |
| 10 | + |
| 11 | +namespace Myvas.AspNetCore.Authentication.WeixinAuth.Internal |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// ref. https://stackoverflow.com/questions/7343465/compression-decompression-string-with-c-sharp |
| 15 | + /// </summary> |
| 16 | + static internal class CompressionExtensions |
| 17 | + { |
| 18 | + public static async Task<IEnumerable<byte>> Zip(this object obj) |
| 19 | + { |
| 20 | + byte[] bytes = obj.Serialize(); |
| 21 | + |
| 22 | + using (MemoryStream msi = new MemoryStream(bytes)) |
| 23 | + using (MemoryStream mso = new MemoryStream()) |
| 24 | + { |
| 25 | + using (var gs = new GZipStream(mso, CompressionMode.Compress)) |
| 26 | + await msi.CopyToAsync(gs); |
| 27 | + |
| 28 | + return mso.ToArray().AsEnumerable(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static async Task<object> Unzip(this byte[] bytes) |
| 33 | + { |
| 34 | + using (MemoryStream msi = new MemoryStream(bytes)) |
| 35 | + using (MemoryStream mso = new MemoryStream()) |
| 36 | + { |
| 37 | + using (var gs = new GZipStream(msi, CompressionMode.Decompress)) |
| 38 | + { |
| 39 | + //gs.CopyTo(mso); |
| 40 | + await gs.CopyToAsync(mso); |
| 41 | + } |
| 42 | + |
| 43 | + return mso.ToArray().Deserialize(); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + internal static class SerializerExtensions |
| 49 | + { |
| 50 | + /// <summary> |
| 51 | + /// Writes the given object instance to a binary file. |
| 52 | + /// <para>Object type (and all child types) must be decorated with the [Serializable] attribute.</para> |
| 53 | + /// <para>To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties.</para> |
| 54 | + /// </summary> |
| 55 | + /// <typeparam name="T">The type of object being written to the XML file.</typeparam> |
| 56 | + /// <param name="filePath">The file path to write the object instance to.</param> |
| 57 | + /// <param name="objectToWrite">The object instance to write to the XML file.</param> |
| 58 | + /// <param name="append">If false the file will be overwritten if it already exists. If true the contents will be appended to the file.</param> |
| 59 | + public static byte[] Serialize<T>(this T objectToWrite) |
| 60 | + { |
| 61 | + using var stream = new MemoryStream(); |
| 62 | + var serializer = new XmlSerializer(typeof(T)); |
| 63 | + serializer.Serialize(stream, objectToWrite); |
| 64 | + return stream.GetBuffer(); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Reads an object instance from a binary file. |
| 69 | + /// </summary> |
| 70 | + /// <typeparam name="T">The type of object to read from the XML.</typeparam> |
| 71 | + /// <param name="filePath">The file path to read the object instance from.</param> |
| 72 | + /// <returns>Returns a new instance of the object read from the binary file.</returns> |
| 73 | + public static async Task<T> _Deserialize<T>(this byte[] arr) |
| 74 | + { |
| 75 | + using var stream = new MemoryStream(arr); |
| 76 | + var serializer = new XmlSerializer(typeof(T)); |
| 77 | + return await Task.FromResult((T)serializer.Deserialize(stream)); |
| 78 | + } |
| 79 | + |
| 80 | + public static async Task<object> Deserialize(this byte[] arr) |
| 81 | + { |
| 82 | + object obj = await arr._Deserialize<object>(); |
| 83 | + return obj; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments