Skip to content

Instantly share code, notes, and snippets.

@shammelburg
Created May 31, 2018 19:16
Show Gist options
  • Save shammelburg/0a97b6bd38be983579409a8fade3d241 to your computer and use it in GitHub Desktop.
Save shammelburg/0a97b6bd38be983579409a8fade3d241 to your computer and use it in GitHub Desktop.
Create a Zip from ASP.NET Core Web API
[HttpGet]
[Route("/api/download/zip/{Code}")]
public async Task<IActionResult> Zip(string Code)
{
byte[] bytes = null;
using (MemoryStream zipStream = new MemoryStream())
{
using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
var files = await _repo.GetForDownloadAsync();
foreach (var file in files.Where(f => f.Code == Code))
{
var path = file.FileUrl.Replace($"{_config["AzureStorage:Url"]}{_config["AzureStorage:ContainerName"]}/", "");
var tempFileName = await _azure.GetFilesByRef(path);
var fi = new FileInfo(tempFileName);
if (fi.Exists)
{
var e = zip.CreateEntryFromFile(fi.FullName, Path.GetFileName(file.FileUrl), CompressionLevel.Fastest);
}
}
}
zipStream.Seek(0, SeekOrigin.Begin);
bytes = zipStream.ToArray();
}
return File(bytes, MediaTypeNames.Application.Zip, $"{Code}_{DateTime.Now.ToString().Replace('/','-')}.zip");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment