-1

We need to bundle a multiple files in a Zip format and download it. Could you please suggest a way to do this in ASP.NET core without using any third party libraries.

In ASP.NET MVC we can achieve this using https://msdn.microsoft.com/en-us/library/system.io.packaging.aspx. Whether is it possible in ASP.NET core 2.0 ?

BALA MURUGAN
  • 1,105
  • 3
  • 11
  • 16
  • There are already multiple answers on this site about that very topic. Search for it and try some of the suggestions. If you get stuck then provide a [mcve] of the issue. – Nkosi Jun 29 '18 at 10:59

1 Answers1

0

I think this will help you a lot

protected FileStreamResult DownloadFolder(string path, string[] names, int count)
{
    FileStreamResult fileStreamResult;
    var tempPath = Path.Combine(Path.GetTempPath(), "temp.zip");
    if (names.Length == 1)
    {
        path = path.Remove(path.Length - 1);
        ZipFile.CreateFromDirectory(path, tempPath, CompressionLevel.Fastest, true);
        FileStream fileStreamInput = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Delete);
        fileStreamResult = new FileStreamResult(fileStreamInput, "APPLICATION/octet-stream");
        fileStreamResult.FileDownloadName = names[0] + ".zip";
    }
    else
    {
        string extension;
        string currentDirectory;
        ZipArchiveEntry zipEntry;
        ZipArchive archive;
        if (count == 0)
        {
            string directory = Path.GetDirectoryName(path);
            string rootFolder = Path.GetDirectoryName(directory);
            using (archive = ZipFile.Open(tempPath, ZipArchiveMode.Update))
            {
                for (var i = 0; i < names.Length; i++)
                {
                    currentDirectory = Path.Combine(rootFolder, names[i]);
                    foreach (var filePath in Directory.GetFiles(currentDirectory, "*.*", SearchOption.AllDirectories))
                    {
                        zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + filePath, names[i] + filePath.Substring(currentDirectory.Length), CompressionLevel.Fastest);
                    }
                }
            }
        }
        else
        {
            string lastSelected = names[names.Length - 1];
            string selectedExtension = Path.GetExtension(lastSelected);
            if (selectedExtension == "")
            {
                path = Path.GetDirectoryName(Path.GetDirectoryName(path));
                path = path.Replace("\\", "/") + "/";

            }
            using (archive = ZipFile.Open(tempPath, ZipArchiveMode.Update))
            {
                for (var i = 0; i < names.Length; i++)
                {
                    extension = Path.GetExtension(names[i]);
                    currentDirectory = Path.Combine(path, names[i]);
                    if (extension == "")
                    {
                        foreach (var filePath in Directory.GetFiles(currentDirectory, "*.*", SearchOption.AllDirectories))
                        {
                            zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + filePath, filePath.Substring(path.Length), CompressionLevel.Fastest);
                        }
                    }
                    else
                    {
                        zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + currentDirectory, names[i], CompressionLevel.Fastest);
                    }
                }
            }

        }
        FileStream fileStreamInput = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Delete);
        fileStreamResult = new FileStreamResult(fileStreamInput, "APPLICATION/octet-stream");
        fileStreamResult.FileDownloadName = "folders.zip";
    }
    if (File.Exists(tempPath))
    {
        File.Delete(tempPath);
    }
    return fileStreamResult;
}
Gehtnet
  • 361
  • 3
  • 19
sureshkumar
  • 144
  • 6
  • 3
    Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer), and also [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) – Anh Pham Oct 16 '18 at 07:51