Skip to content

Instantly share code, notes, and snippets.

@mebjas
Last active April 3, 2018 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mebjas/7029246d68bc90451678af683fbf1f31 to your computer and use it in GitHub Desktop.
Save mebjas/7029246d68bc90451678af683fbf1f31 to your computer and use it in GitHub Desktop.
zip to blob
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