How to unzip a file in memory using an Azure CloudBlob

  • Thread starter Thread starter Jack Stormbringer
  • Start date Start date
J

Jack Stormbringer

Guest
Azure doesn't allow me to unzip a file directly in a container. I downloaded a zip file and now need to expand the file in the zip file. What I get is a 0 byte file. I can download the zip to my local computer and see the embedded csv, so the zip file isn't corrupt. I get no errors, just a zero byte output file. What am I doing wrong? I've tried all of these options unsuccessfully:

using (MemoryStream ms = new MemoryStream())
{
await zipOutputBlob.DownloadToStreamAsync(ms);
using (var zipStream = new GZipStream(ms, CompressionMode.Decompress))
{
CloudBlockBlob unzippedBlob = container.GetBlockBlobReference(String.Format("{0:yyyy-MM-dd}", lastWriteTime) + " " + Path.GetFileNameWithoutExtension(fileName) + ".csv");
unzippedBlob.Properties.ContentType = "text/csv";
using (Stream outputFileStream = await unzippedBlob.OpenWriteAsync())
{
await zipStream.CopyToAsync(outputFileStream);
outputFileStream.Flush();
}
}
}

2nd try:

using (MemoryStream ms = new MemoryStream())
{
await zipOutputBlob.DownloadToStreamAsync(ms);
using (var zipStream = new GZipStream(ms, CompressionMode.Decompress))
{
CloudBlockBlob unzippedBlob = container.GetBlockBlobReference(String.Format("{0:yyyy-MM-dd}", lastWriteTime) + " " + Path.GetFileNameWithoutExtension(fileName) + ".csv");
unzippedBlob.Properties.ContentType = "text/csv";
await unzippedBlob.UploadFromStreamAsync(zipStream);
}
}

3rd

using (MemoryStream ms = new MemoryStream())
{
await zipOutputBlob.DownloadToStreamAsync(ms);
using (var zipStream = new GZipStream(ms, CompressionMode.Decompress))
{
CloudBlockBlob unzippedBlob = container.GetBlockBlobReference(String.Format("{0:yyyy-MM-dd}", lastWriteTime) + " " + Path.GetFileNameWithoutExtension(fileName) + ".csv");
unzippedBlob.Properties.ContentType = "text/csv";
using (Stream outputFileStream = await unzippedBlob.OpenWriteAsync())
{
await zipStream.CopyToAsync(outputFileStream);
outputFileStream.Flush();
}
}
}

4th

using (MemoryStream ms = new MemoryStream())
{
await zipOutputBlob.DownloadToStreamAsync(ms);
using (DeflateStream decompressionStream = new DeflateStream(ms, CompressionMode.Decompress))
{
CloudBlockBlob unzippedBlob = container.GetBlockBlobReference(String.Format("{0:yyyy-MM-dd}", lastWriteTime) + " " + Path.GetFileNameWithoutExtension(fileName) + ".csv");
unzippedBlob.Properties.ContentType = "text/csv";
using (Stream outputFileStream = await unzippedBlob.OpenWriteAsync())
{
await decompressionStream.CopyToAsync(outputFileStream);
outputFileStream.Flush();
}
}
}

5th

using (var inputStream = new MemoryStream())
{
await zipOutputBlob.DownloadToStreamAsync(inputStream);
inputStream.Seek(0, SeekOrigin.Begin);

using (var gzStream = new GZipStream(inputStream, CompressionMode.Decompress))
{
using (var outputStream = new MemoryStream())
{
gzStream.CopyTo(outputStream);
byte[] outputBytes = outputStream.ToArray(); // No data. Sad panda. :'(
string output = Encoding.ASCII.GetString(outputBytes);
CloudBlockBlob unzippedBlob = container.GetBlockBlobReference(String.Format("{0:yyyy-MM-dd}", lastWriteTime) + " " + Path.GetFileNameWithoutExtension(fileName) + ".csv");
unzippedBlob.Properties.ContentType = "text/csv";
await unzippedBlob.UploadTextAsync(output);
}
}
}

6th

using (var ms = new MemoryStream())
{
await zipOutputBlob.DownloadToStreamAsync(ms);
ms.Seek(0, SeekOrigin.Begin);

using (DeflateStream decompressionStream = new DeflateStream(ms, CompressionMode.Decompress))
{
using (var outputStream = new MemoryStream())
{
decompressionStream.CopyTo(outputStream);
byte[] outputBytes = outputStream.ToArray(); // No data. Sad panda. :'(
string output = Encoding.ASCII.GetString(outputBytes);
CloudBlockBlob unzippedBlob = container.GetBlockBlobReference(String.Format("{0:yyyy-MM-dd}", lastWriteTime) + " " + Path.GetFileNameWithoutExtension(fileName) + ".csv");
unzippedBlob.Properties.ContentType = "text/csv";
await unzippedBlob.UploadTextAsync(output);
}
}
}

Option 5 and 6 fail with this error message on the CopyTo method:
> System.Private.CoreLib: Exception while executing function: DefinitiveHealthCare. System.IO.Compression: The archive entry was compressed using an unsupported compression method.

How is this done?

Continue reading...
 
Back
Top