简体   繁体   English

打开使用EPPlus创建的.xlsx文件并使用ICSharpCode.SharpZipLib压缩到一个文件夹时出现问题

[英]Problems opening .xlsx file created with EPPlus and zipped in a folder with ICSharpCode.SharpZipLib

I am creating a list of ExcelPackages (xlsx documents) using EPPlus, and adding them to a ZipOutputStream as ZipEntries. 我正在使用EPPlus创建ExcelPackages(xlsx文档)列表,并将它们作为ZipEntries添加到ZipOutputStream。 I think the Excel documents should be valid, as I can open them just fine when I write one of them to the Response object without zipping. 我认为Excel文档应该是有效的,因为当我将其中一个文件写入Response对象而不进行压缩时,我可以正常打开它们。
The zip folder is created as expected, and the file(s) are there and doesnt seem to be empty, but when I try to open them I get the following error in Excel: zip文件夹是按预期创建的,文件在那里并且似乎没有空,但是当我尝试打开它们时,我在Excel中收到以下错误:

Excel cannot open the file {Name}.xlsx because the file format or file extension is not valid. Excel无法打开文件{Name} .xlsx,因为文件格式或文件扩展名无效。 Verify that file has not been corrupted and that the file extension matches the format of the file 验证文件是否已损坏,以及文件扩展名是否与文件格式匹配

List<ExcelPackage> documents = new List<ExcelPackage>();
List<string> fileNames = new List<string>();

//Code for fetching documents and filenames here (removed for the sake of readability)

Response.Clear();
Context.Response.BufferOutput = false; 
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment; filename=\"random-foldername.zip\"");
Response.CacheControl = "Private";
Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); 

ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);
zipOutputStream.SetLevel(3); //0-9, 9 being the highest level of compression
byte[] buffer = null;

for (int i = 0; i < documents.Count; i++)
{
    MemoryStream ms = new MemoryStream();

    documents[i].SaveAs(ms);

    ZipEntry entry = new ZipEntry(ZipEntry.CleanName(fileNames[i]));

    zipOutputStream.PutNextEntry(entry);
    buffer = new byte[ms.Length];

    ms.Read(buffer, 0, buffer.Length);
    entry.Size = ms.Length;

    ms.Close();

    zipOutputStream.Write(buffer, 0, buffer.Length);
    zipOutputStream.CloseEntry();

}
zipOutputStream.Finish();
zipOutputStream.Close();

Response.End();

As for the list of filenames, im just generating a name based on some arbitrary stuff and adding a ".xlsx"-extension to the end of it. 至于文件名列表,我只是根据一些任意的东西生成一个名称,并在其末尾添加一个“.xlsx” - 扩展。

Im not sure where im going wrong here, any suggestions? 我不知道我在哪里出错了,有什么建议吗?

You have to rewind memory stream before you can read something (after write operation file pointer is that its end): 您必须先回滚内存流,然后才能读取内容(在写入操作文件指针之后):

ms.Seek(0, SeekOrigin.Begin)
ms.Read(buffer, 0, buffer.Length);

That said a MemoryStream is nothing more than a byte array so you don't even need to allocate and read a new one then this code: 这就是说MemoryStream只不过是一个字节数组,所以你甚至不需要分配和读取一个新的代码:

buffer = new byte[ms.Length];

ms.Read(buffer, 0, buffer.Length);
entry.Size = ms.Length;

ms.Close();

zipOutputStream.Write(buffer, 0, buffer.Length);

Can be simply replaced with: 可以简单地替换为:

entry.Size = ms.Length;
zipOutputStream.Write(ms.GetBuffer(), 0, ms.Length);
ms.Close();

Final note: if you don't want to use internal MemoryStream buffer (for any reason) and you want a trimmed copy of it (as you're doing manually) then simply use ToArray() method like this: 最后注意:如果您不想使用内部MemoryStream缓冲区(出于任何原因)并且您想要它的修剪副本(当您手动执行时),那么只需使用ToArray()方法,如下所示:

var buffer = ms.ToArray();
ms.Close();
entry.Size = buffer.Length;
zipOutputStream.Write(buffer, 0, buffer.Length);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ICSharpCode.SharpZipLib验证zip文件 - ICSharpCode.SharpZipLib validate zip file 如何使用ICSharpCode.SharpZipLib将文件夹添加到zip存档 - How do you add a folder to a zip archive with ICSharpCode.SharpZipLib 如何使用 ICSharpCode.SharpZipLib 压缩文件数据并使用 HttpClient 上传? - How to compress file data with ICSharpCode.SharpZipLib and upload using HttpClient? ICSharpCode.SharpZipLib 压缩方法不支持 PKZIP - ICSharpCode.SharpZipLib Compression method not supported PKZIP 无法加载文件或程序集 ICSharpCode.SharpZipLib... 当使用 nuGet 包 ExcelDataReader - Could not load file or assembly ICSharpCode.SharpZipLib… When using nuGet package ExcelDataReader 无法加载文件或程序集“ICSharpCode.SharpZipLib”或其依赖项之一。 访问被拒绝 - Could not load file or assembly 'ICSharpCode.SharpZipLib' or one of its dependencies. Access is denied 发现无法解决的“ ICSharpCode.SharpZipLib”不同版本之间的冲突 - Found conflicts between different versions of “ICSharpCode.SharpZipLib” that could not be resolved 使用ICSharpCode.SharpZipLib的项目的SQLCLR发布失败 - SQLCLR-Publish fail for project using ICSharpCode.SharpZipLib 使用c#ICSharpCode.SharpZipLib将目录中的大量文件压缩 - Zipping large quantity of files in a directory using c# ICSharpCode.SharpZipLib icsharpcode SharpZipLib没有在zip文件上正确设置密码 - icsharpcode SharpZipLib is not setting password correctly on zip file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM