Skip to content

Instantly share code, notes, and snippets.

@mebjas
Created 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/e5bdf6390c32f9992cb7d4de9a75f9af to your computer and use it in GitHub Desktop.
Save mebjas/e5bdf6390c32f9992cb7d4de9a75f9af to your computer and use it in GitHub Desktop.
BlobUnzip.cs
public DataModel BlobToMemoryStream(string blobName)
{
CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(blobName);
using (MemoryStream ms = new MemoryStream())
{
blockBlob.DownloadToStream(ms);
ms.Seek(0, SeekOrigin.Begin);
ms.Flush();
ms.Position = 0;
byte[] data = ms.ToArray();
using (var msi = new MemoryStream(data))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
CopyTo(gs, mso);
}
string jsonString = Encoding.UTF8.GetString(mso.ToArray());
msi.Dispose();
return JsonConvert.DeserializeObject<DataModel>(jsonString);
}
}
}
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