我使用ZipArchive压缩了字节数组。在执行以下方法时,它提示您保存压缩的文件夹,但在尝试打开文件夹时提示“窗口无法打开该文件夹。压缩的(压缩的)文件夹无效。”错误。以下是压缩文件的代码。
public void CompressFile() {
using (var compressedFileStream = new MemoryStream())
//Create an archive and store the stream in memory.
using (var zipArchive = new ZipArchive(compressedFileStream,ZipArchiveMode.Create, true))
{
//Create a zip entry for each attachment
var zipEntry = zipArchive.CreateEntry("FY14 statements");
byte[] pdf = _dmsbl.DocumentData(4);
//Get the stream of the attachment
using (var originalFileStream = new MemoryStream(pdf))
using (System.IO.Stream entryStream = zipEntry.Open())
{
originalFileStream.CopyTo(entryStream);
}
SendOutZip(compressedFileStream.ToArray(), "FileName.zip");
}
}
private void SendOutZip(byte[] zippedFiles, string filename)
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/x-compressed";
Response.Charset = string.Empty;
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.BinaryWrite(zippedFiles);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.End();
}