thecaptain0220
thecaptain0220

Reputation: 2168

Issues downloading zip files ASP MVC

I am working with an ASP MVC site that zips some files together. It seems to be working but when I try to extract the files I get

An unexpected error is keeping you from copying the file. If you continue the receive this error, you can use the error code to search for help with this problem.
enter code here


Error 0x80004005: Unspecified error

7 Zip says CRC failed. File is broken.

It seems that somthing is getting corrupted either when writing or downloading the zip. I have tried 2 different methods to download the file with the same result.

public ActionResult DownloadFiles()
{
    List<string> files = GetFileList();
    using (ZipFile zip = new ZipFile())
    {
        foreach (string file in files)
            zip.AddFile(file, string.Empty);

        MemoryStream output = new MemoryStream();
        zip.Save(output);
        return File(output.ToArray(), "application/zip", "file.zip");
    }
}

public void DownloadFiles()
{
    List<string> files = GetFileList();
    using (ZipFile zip = new ZipFile())
    {
        foreach (string file in files)
            zip.AddFile(file, string.Empty);

        Response.Clear();
        Response.BufferOutput = false;
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "attachment; filename=file.zip");
        zip.Save(Response.OutputStream);
        Response.End();
    }
}

<button onclick="fn_DownloadFiles()">Download Selected</button>

function fn_DownloadSelectedFiles() 
{
    window.location.href = "/Files/DownloadFiles"
}

Any reason why the files would be getting corrupted?

I tried this too

public ActionResult DownloadFiles(string filePaths)
{
    List<string> files = GetFileList();
    using (MemoryStream zipStream = new MemoryStream())
    using (ZipArchive zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
    {
        foreach (string file in files)
        {
            ZipArchiveEntry entry = zipArchive.CreateEntry(file, CompressionLevel.Fastest);

            using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            using (Stream entryStream = entry.Open())
                fileStream.CopyTo(entryStream);
        }
        return File(zipStream.ToArray(), "application/zip", "files.zip"));
    }
}

With this I get Windows cannot open the folder. The compressed (zipped) Folder is invalid.

Upvotes: 2

Views: 2172

Answers (2)

Nagaraj Raveendran
Nagaraj Raveendran

Reputation: 1220

What version of DotNetZip you are using? There is a bug in the recent version. Try installing an older version(1.9) and run your code. If it works, then the bug is still not fixed.

Below is the link of the workitem. https://dotnetzip.codeplex.com/workitem/14087

Upvotes: 1

Igor
Igor

Reputation: 62213

I tested with the following code and saw no issues at all. The following code is a complete working example after installing the latest NuGet package DotNetZip. I have no idea what GetFiles did so I replaced it with some files I picked up from a hard coded directory. I would guess that your problem is probably there so maybe you can share that code. Again, this is a test using an MVC controller that you had illustrated where I only changed that one thing to show that there is no problem

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Ionic.Zip;

namespace MvcTesting.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            // replace C:\PickAFolderWithSomeFiles\ with a folder that has some files that you can zip to test
            List<string> files = System.IO.Directory.GetFiles(@"C:\PickAFolderWithSomeFiles\").ToList();
            using (ZipFile zip = new ZipFile())
            {
                foreach (string file in files)
                    zip.AddFile(file, string.Empty);

                using (MemoryStream output = new MemoryStream())
                {
                    zip.Save(output);
                    return File(output.ToArray(), "application/zip", "file.zip");
                }
            }
        }
    }
}

Upvotes: 1

Related Questions