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.