Last active
April 3, 2018 20:02
-
-
Save mebjas/7029246d68bc90451678af683fbf1f31 to your computer and use it in GitHub Desktop.
zip to blob
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
public void UploadDataModel(DataModel model) | |
{ | |
MemoryStream stream = new MemoryStream(); | |
using (var sw = new StreamWriter(stream)) | |
using (JsonWriter writer = new JsonTextWriter(sw)) | |
{ | |
var serializer = new JsonSerializer(); | |
serializer.Serialize(writer, model); | |
sw.Flush(); | |
stream.Position = 0; | |
using (var mso = new MemoryStream()) | |
{ | |
using (var gs = new GZipStream(mso, CompressionMode.Compress)) | |
{ | |
CopyTo(stream, gs); | |
} | |
mso.Flush(); | |
byte[] data = mso.ToArray(); | |
CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(blobName); | |
blockBlob.UploadFromByteArray(data, 0, data.Length); | |
} | |
} | |
} | |
void CopyTo(Stream src, Stream dest) | |
{ | |
byte[] bytes = new byte[4096]; | |
int cnt; | |
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) | |
{ | |
dest.Write(bytes, 0, cnt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment