BALA MURUGAN
BALA MURUGAN

Reputation: 2063

How to create and download a zip file in ASP.NET Core?

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 ?

Upvotes: 0

Views: 7150

Answers (1)

sureshkumar
sureshkumar

Reputation: 184

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;
}

Upvotes: 2

Related Questions