| | | 1 | | using System.Text; |
| | | 2 | | using IronCompress; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Common.Codecs; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a ZSTD compression strategy. |
| | | 8 | | /// </summary> |
| | | 9 | | public class Zstd : ICompressionCodec |
| | | 10 | | { |
| | 1 | 11 | | private Iron Iron { get; set; } = new(); |
| | | 12 | | |
| | | 13 | | /// <inheritdoc /> |
| | | 14 | | public ValueTask<string> CompressAsync(string input, CancellationToken cancellationToken = default) |
| | | 15 | | { |
| | 0 | 16 | | var inputBytes = Encoding.UTF8.GetBytes(input); |
| | 0 | 17 | | var span = inputBytes.AsSpan(); |
| | 0 | 18 | | var result = Iron.Compress(Codec.Zstd, span); |
| | 0 | 19 | | var compressedBytes = result.AsSpan(); |
| | 0 | 20 | | var compressedString = Convert.ToBase64String(compressedBytes); |
| | | 21 | | |
| | 0 | 22 | | return new (compressedString); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public ValueTask<string> DecompressAsync(string input, CancellationToken cancellationToken = default) |
| | | 27 | | { |
| | 0 | 28 | | var inputBytes = Convert.FromBase64String(input); |
| | 0 | 29 | | var span = inputBytes.AsSpan(); |
| | 0 | 30 | | var result = Iron.Decompress(Codec.Zstd, span); |
| | 0 | 31 | | var decompressedBytes = result.AsSpan(); |
| | 0 | 32 | | var decompressedString = Encoding.UTF8.GetString(decompressedBytes); |
| | | 33 | | |
| | 0 | 34 | | return new (decompressedString); |
| | | 35 | | } |
| | | 36 | | } |