-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJZipAppendBlob.cs
73 lines (60 loc) · 2.7 KB
/
JZipAppendBlob.cs
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
70
71
72
73
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using System.IO.Compression;
using System.Text;
namespace JZipBlob
{
public static class JZipAppendBlob
{
/// <summary>
/// Compress JSON data as a string of List<object[]> and append the blob
/// </summary>
/// <param name="appendBlobClient">Azure.Storage.Blobs.AppendBlobClient</param>
/// <param name="data"></param>
/// <param name="encodingCode">Any valid ecoding code, 65001 = utf8; 1200 = Unicode; 12000 = utf32</param>
/// <returns></returns>
public static async Task CompressAndAppendBlobAsync(AppendBlobClient appendBlobClient, string data, int encodingCode = 65001)
{
byte[] bytes = Encoding.GetEncoding(encodingCode).GetBytes(data);
using Stream stream = await appendBlobClient.OpenWriteAsync(false);
using var gzipStream = new GZipStream(stream, CompressionLevel.Optimal);
await gzipStream.WriteAsync(bytes);
}
/// <summary>
/// Decompress JSON data as a string of List<object[]> from an append blob
/// </summary>
/// <param name="blocClient">Azure.Storage.Blobs.BlobClient</param>
/// <param name="isSealed">Not yet implemented - Use true to get data from an open append blob.
/// Use false to get data from a closed or sealed append blob, else use null and a check will be done</param>
/// <param name="encodingCode">Any valid ecoding code, 65001 = utf8; 1200 = Unicode; 12000 = utf32</param>
/// <returns></returns>
public static async Task<string> DecompressAndDownloadAppendBlobAsync(BlobClient blocClient, int encodingCode = 65001)//bool? isSealed = null)
{
using var memoryStream = new MemoryStream();
using (var decompressStream = new GZipStream(await blocClient.OpenReadAsync(new BlobOpenReadOptions(false)), CompressionMode.Decompress))
{
await decompressStream.CopyToAsync(memoryStream);
}
memoryStream.WriteByte((byte)']');
byte[] arr = memoryStream.ToArray();
//if (!isSealed.HasValue)
//{
// throw new NotImplementedException();
//}
//if (!isSealed.Value)
//{
//arr[^1] = (byte)']';
//}
return Encoding.GetEncoding(encodingCode).GetString(arr);
}
public static void InitialAppend(StringBuilder sb)
{
sb.Append('[');
}
public static void StartRow(StringBuilder sb)
{
sb.Append(',');
}
}
}