-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfb8c6d
commit 1f0964e
Showing
16 changed files
with
2,102 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"sdk": { | ||
"version": "6.0.100", | ||
"version": "7.0.100", | ||
"rollForward": "latestFeature" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Myvas.AspNetCore.Authentication.WeixinAuth.Internal | ||
{ | ||
/// <summary> | ||
/// ref. https://stackoverflow.com/questions/7343465/compression-decompression-string-with-c-sharp | ||
/// </summary> | ||
static internal class CompressionExtensions | ||
{ | ||
public static async Task<IEnumerable<byte>> Zip(this object obj) | ||
{ | ||
byte[] bytes = obj.Serialize(); | ||
|
||
using (MemoryStream msi = new MemoryStream(bytes)) | ||
using (MemoryStream mso = new MemoryStream()) | ||
{ | ||
using (var gs = new GZipStream(mso, CompressionMode.Compress)) | ||
await msi.CopyToAsync(gs); | ||
|
||
return mso.ToArray().AsEnumerable(); | ||
} | ||
} | ||
|
||
public static async Task<object> Unzip(this byte[] bytes) | ||
{ | ||
using (MemoryStream msi = new MemoryStream(bytes)) | ||
using (MemoryStream mso = new MemoryStream()) | ||
{ | ||
using (var gs = new GZipStream(msi, CompressionMode.Decompress)) | ||
{ | ||
//gs.CopyTo(mso); | ||
await gs.CopyToAsync(mso); | ||
} | ||
|
||
return mso.ToArray().Deserialize(); | ||
} | ||
} | ||
} | ||
|
||
internal static class SerializerExtensions | ||
{ | ||
/// <summary> | ||
/// Writes the given object instance to a binary file. | ||
/// <para>Object type (and all child types) must be decorated with the [Serializable] attribute.</para> | ||
/// <para>To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties.</para> | ||
/// </summary> | ||
/// <typeparam name="T">The type of object being written to the XML file.</typeparam> | ||
/// <param name="filePath">The file path to write the object instance to.</param> | ||
/// <param name="objectToWrite">The object instance to write to the XML file.</param> | ||
/// <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> | ||
public static byte[] Serialize<T>(this T objectToWrite) | ||
{ | ||
using (MemoryStream stream = new MemoryStream()) | ||
{ | ||
BinaryFormatter binaryFormatter = new BinaryFormatter(); | ||
binaryFormatter.Serialize(stream, objectToWrite); | ||
|
||
return stream.GetBuffer(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Reads an object instance from a binary file. | ||
/// </summary> | ||
/// <typeparam name="T">The type of object to read from the XML.</typeparam> | ||
/// <param name="filePath">The file path to read the object instance from.</param> | ||
/// <returns>Returns a new instance of the object read from the binary file.</returns> | ||
public static async Task<T> _Deserialize<T>(this byte[] arr) | ||
{ | ||
using (MemoryStream stream = new MemoryStream()) | ||
{ | ||
BinaryFormatter binaryFormatter = new BinaryFormatter(); | ||
await stream.WriteAsync(arr, 0, arr.Length); | ||
stream.Position = 0; | ||
|
||
return (T)binaryFormatter.Deserialize(stream); | ||
} | ||
} | ||
|
||
public static async Task<object> Deserialize(this byte[] arr) | ||
{ | ||
object obj = await arr._Deserialize<object>(); | ||
return obj; | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Serialization; | ||
|
||
namespace Myvas.AspNetCore.Authentication.WeixinAuth.Internal | ||
{ | ||
/// <summary> | ||
/// ref. https://stackoverflow.com/questions/7343465/compression-decompression-string-with-c-sharp | ||
/// </summary> | ||
static internal class CompressionExtensions | ||
{ | ||
public static async Task<IEnumerable<byte>> Zip(this object obj) | ||
{ | ||
byte[] bytes = obj.Serialize(); | ||
|
||
using (MemoryStream msi = new MemoryStream(bytes)) | ||
using (MemoryStream mso = new MemoryStream()) | ||
{ | ||
using (var gs = new GZipStream(mso, CompressionMode.Compress)) | ||
await msi.CopyToAsync(gs); | ||
|
||
return mso.ToArray().AsEnumerable(); | ||
} | ||
} | ||
|
||
public static async Task<object> Unzip(this byte[] bytes) | ||
{ | ||
using (MemoryStream msi = new MemoryStream(bytes)) | ||
using (MemoryStream mso = new MemoryStream()) | ||
{ | ||
using (var gs = new GZipStream(msi, CompressionMode.Decompress)) | ||
{ | ||
//gs.CopyTo(mso); | ||
await gs.CopyToAsync(mso); | ||
} | ||
|
||
return mso.ToArray().Deserialize(); | ||
} | ||
} | ||
} | ||
|
||
internal static class SerializerExtensions | ||
{ | ||
/// <summary> | ||
/// Writes the given object instance to a binary file. | ||
/// <para>Object type (and all child types) must be decorated with the [Serializable] attribute.</para> | ||
/// <para>To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties.</para> | ||
/// </summary> | ||
/// <typeparam name="T">The type of object being written to the XML file.</typeparam> | ||
/// <param name="filePath">The file path to write the object instance to.</param> | ||
/// <param name="objectToWrite">The object instance to write to the XML file.</param> | ||
/// <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> | ||
public static byte[] Serialize<T>(this T objectToWrite) | ||
{ | ||
using var stream = new MemoryStream(); | ||
var serializer = new XmlSerializer(typeof(T)); | ||
serializer.Serialize(stream, objectToWrite); | ||
return stream.GetBuffer(); | ||
} | ||
|
||
/// <summary> | ||
/// Reads an object instance from a binary file. | ||
/// </summary> | ||
/// <typeparam name="T">The type of object to read from the XML.</typeparam> | ||
/// <param name="filePath">The file path to read the object instance from.</param> | ||
/// <returns>Returns a new instance of the object read from the binary file.</returns> | ||
public static async Task<T> _Deserialize<T>(this byte[] arr) | ||
{ | ||
using var stream = new MemoryStream(arr); | ||
var serializer = new XmlSerializer(typeof(T)); | ||
return await Task.FromResult((T)serializer.Deserialize(stream)); | ||
} | ||
|
||
public static async Task<object> Deserialize(this byte[] arr) | ||
{ | ||
object obj = await arr._Deserialize<object>(); | ||
return obj; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.