検索してStack overFlowやMicorosft公式ドキュメントどおりに作成したけどZipの中身が空っぽで小一時間悩んでやっと解決したのでメモ書き
[HttpGet("{id}/zip")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetZipById(int id)
{
Blob record = // DbContextからBlobを取得
if (record == null) return NotFound();
using var memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var entry = archive.CreateEntry($"file{record.Extension}", CompressionLevel.Optimal);
using (var es = entry.Open())
{
es.Write(record.Bytes!, 0, record.Bytes!.Length);
}
}
return File(memoryStream.ToArray(), "application/zip", $"file.zip");
}
public record Blob
{
public int Id { get; set; }
public byte[]? Bytes { get; set; } = Array.Empty<byte>();
public string Contextype { get; set; } = string.Empty;
public string Extension { get; set; } = string.Empty;
}