创建在从字节数组控制器zip文件和我返回zip文件作为fileresult。 当我下载ZIP文件并解压文件时,它已损坏。 我做这样说:
byte[] fileBytes =array
MemoryStream fileStream = new MemoryStream(fileBytes);
MemoryStream outputStream = new MemoryStream();
fileStream.Seek(0, SeekOrigin.Begin);
using (ZipFile zipFile = new ZipFile())
{
zipFile.AddEntry(returnFileName, fileStream);
zipFile.Save(outputStream);
}
outputStream.Position = 0;
FileStreamResult fileResult = new FileStreamResult(outputStream, System.Net.Mime.MediaTypeNames.Application.Zip);
fileResult.FileDownloadName = returnFileName + ".zip";
return fileResult;
你可能会不走运击中DotNetZip的开放的错误之一。 还有例如取决于文件大小(一期https://dotnetzip.codeplex.com/workitem/14087 )。
不幸的是,DotNetZip有一些关键问题和项目似乎不再积极进行维护。 更好的办法是使用SharpZipLib(如果你符合他们的基于GPL的许可证),或之一的zlib .NET端口 。
如果你在.NET 4.5,你可以在使用内置类System.IO.Compression
命名空间。 下面的示例可以在的文档中找到ZipArchive
类:
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (var zipToOpen =
new FileStream(@"c:\tmp\release.zip", FileMode.Open))
{
using (var archive =
new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
var readmeEntry = archive.CreateEntry("Readme.txt");
using (var writer = new StreamWriter(readmeEntry.Open()))
{
writer.WriteLine("Information about this package.");
writer.WriteLine("========================");
}
}
}
}
}
}
public class HomeController : Controller
{
public FileResult Index()
{
FileStreamResult fileResult = new FileStreamResult(GetZippedStream(), System.Net.Mime.MediaTypeNames.Application.Zip);
fileResult.FileDownloadName = "result" + ".zip";
return fileResult;
}
private static Stream GetZippedStream()
{
byte[] fileBytes = Encoding.ASCII.GetBytes("abc");
string returnFileName = "something";
MemoryStream fileStream = new MemoryStream(fileBytes);
MemoryStream resultStream = new MemoryStream();
using (ZipFile zipFile = new ZipFile())
{
zipFile.AddEntry(returnFileName, fileStream);
zipFile.Save(resultStream);
}
resultStream.Position = 0;
return resultStream;
}
}