Search code examples
c#streamzipazure-blob-storagecsvhelper

zipping in-stream to a csv using ZipArchive, CsvHelper, and Azure Blobs leaves me with an empty archive C#


I am trying to upload the zipped the results of an sql query converted to a csv to a blob.

See my code below:

  //this doesn't work
  using (var sqlCommand = _dataPointService.OpenSqlConnectionForCsvQuery(device, start, end))
  using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
  using (var blobWriteStream = appendBlobClient.OpenWrite(true))
  using (var zipArchive = new ZipArchive(blobWriteStream, ZipArchiveMode.Create))
  using (var streamWriter = new StreamWriter(zipArchive.CreateEntry(fileName + ".csv").Open()))
  using (var csvWriter = new CsvWriter(streamWriter, new CsvConfiguration(CultureInfo.InvariantCulture)))
  { 
    var generator = new StreamCsvGenerator(device, start, end, showTemperatureInFahrenheit);

    generator.FeedHeader(csvWriter);

    {
      while (sqlDataReader.Read())
      {
        generator.FeedRow(csvWriter, sqlDataReader);
      }
    }

    streamWriter.Flush();
  }

I end up with the following empty archive in my blob: 0 filesize archive

However if I don't bother with zipping, everything works out:

 //this works
  using (var sqlCommand = _dataPointService.OpenSqlConnectionForCsvQuery(device, start, end))
  using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
  using (var blobWriteStream = appendBlobClient.OpenWrite(true))
  using (var streamWriter = new StreamWriter(blobWriteStream))
  using (var csvWriter = new CsvWriter(streamWriter, new CsvConfiguration(CultureInfo.InvariantCulture)))
  {
    var generator = new StreamCsvGenerator(device, start, end, showTemperatureInFahrenheit);

    generator.FeedHeader(csvWriter);

    {
      while (sqlDataReader.Read())
      {
        generator.FeedRow(csvWriter, sqlDataReader);
      }
    }

    streamWriter.Flush();
  }

Here's what the result looks like: enter image description here

Am I using the ZipArchive / ZipEntry streams incorrectly? How can I fix it?


Solution

  • The fix was to update my Azure.Storage.Blobs package to latest.

    I was on version 12.6.0. Once I updated to 12.8.3, everything worked fine.

    Here's how my call looks for anyone coming across this in the future:

          var appendBlobClient = BlobStorage.CreateAppendBlobClient(blobName);
          
          using (var sqlCommand = _dataPointService.OpenSqlConnectionForCsvQuery(device, start, end))
          using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
          using (var blobWriteStream = appendBlobClient.OpenWrite(true))
          using (var zipArchive = new ZipArchive(blobWriteStream, ZipArchiveMode.Create))
          using (var zipEntryStream = zipArchive.CreateEntry(fileName + ".csv").Open())
          using (var streamWriter = new StreamWriter(zipEntryStream))
          using (var csvWriter = new CsvWriter(streamWriter, new CsvConfiguration(CultureInfo.CurrentCulture)))
          {
            // this class is a csvWriter helper
            var generator = new StreamCsvGenerator(device, start, end, showTemperatureInFahrenheit);
    
            generator.FeedHeader(csvWriter); // this writes some comments at the top of the csv file
            
            while (sqlDataReader.Read())
            {
              generator.FeedRow(csvWriter, sqlDataReader); // this writes records
            }
          }