I'm creating a web api route that takes a list of byte arrays and creats a zip file out of it. However the returned file is corrupted and im unable to extract it's content using a Windows default zip extractor (I'm able to open and extract it correctly using 7-zip however). What could be a possible reason for that?
using (var stream = new MemoryStream())
using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create))
{
var i = 1;
foreach(var file in files)
{
var zipEntry = zipArchive.CreateEntry($"{i}.txt");
using (var zipStream = zipEntry.Open())
{
zipStream.Write(file, 0, file.Length);
}
i++;
}
stream.Seek(0, SeekOrigin.Begin);
return new FileContentResult(stream.ToArray(), "application/octet-stream") { FileDownloadName = "Splitted.zip" };
}
Could anyone guide me on what's I'm doing wrong, whilst I'm creating a zip archive?
Edit: I even tried creating a zip with a single text file containing a 'hello' string and it still failed to open:
[HttpGet]
[Route("asd")]
public IActionResult Getasd()
{
using (var stream = new MemoryStream())
using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create))
{
var text = "hello";
var bytes = Encoding.UTF8.GetBytes(text);
var zipEntry = zipArchive.CreateEntry($"asd.txt", CompressionLevel.NoCompression);
using (var memStream = new MemoryStream(bytes))
using (var zipStream = zipEntry.Open())
{
memStream.CopyTo(zipStream);
}
return new FileContentResult(stream.ToArray(), "application/octet-stream") { FileDownloadName = "Splitted.zip" };
}
}