Created
April 3, 2018 20:02
-
-
Save mebjas/e5bdf6390c32f9992cb7d4de9a75f9af to your computer and use it in GitHub Desktop.
BlobUnzip.cs
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 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