Home > Mobile >  Decrypting and Decompressing Data from Azure Blob Storage using C#
Decrypting and Decompressing Data from Azure Blob Storage using C#

Time:11-04

I am using following code to Encrypt and Compress the Data before uploading to Azure Blob Storage

           //calling the API for Data
            var response = httpclient.Send(webRequest);

           //Compressing 
            using MemoryStream compressedMemoryStream = new MemoryStream();
            using (Stream bodyStream = response.Content.ReadAsStream())
            {
                using (GZipStream compressionStream = new GZipStream(compressedMemoryStream,
                CompressionMode.Compress, true))
                {
                    await bodyStream.CopyToAsync(compressionStream);
                }
            }
            compressedMemoryStream.Position = 0;

            //Uploading to blob 
            //options includes the details about encrypting
            blob.UploadFromStream(compressedMemoryStream, compressedMemoryStream.Length, null, options, null);

With this Data is being uploaded successfully to Azure Blob

But I when try to download -> decrypt -> decompress it is giving me empty data

Code below

            
            var compressedStream = new MemoryStream();

            //Download and decrypt Blob data to MemoryStream
            dblob.DownloadToStream(compressedStream, null, doptions, null);

           //Decompress Code
            var bigStream = new GZipStream(compressedStream, CompressionMode.Decompress);
            var bigStreamOut = new MemoryStream();
            bigStream.CopyTo(bigStreamOut);
            output = Encoding.UTF8.GetString(bigStreamOut.ToArray());
            

            // “output is empty".
            Console.WriteLine(output);

CodePudding user response:

You need to set the position to 0. The following code should work:

var compressedStream = new MemoryStream();

//Download and decrypt Blob data to MemoryStream
dblob.DownloadToStream(compressedStream, null, doptions, null);
compressedStream.Position = 0;

//Decompress Code
var bigStream = new GZipStream(compressedStream, CompressionMode.Decompress);
var bigStreamOut = new MemoryStream();
bigStream.CopyTo(bigStreamOut);
output = Encoding.UTF8.GetString(bigStreamOut.ToArray());


// “output is empty".
Console.WriteLine(output);
  • Related