Instantly share code, notes, and snippets.

Embed
What would you like to do?
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