C# 预览由OpenXML创建的zip文件中的文档时出错

C# 预览由OpenXML创建的zip文件中的文档时出错,c#,openxml,httpresponse,attachment,C#,Openxml,Httpresponse,Attachment,我使用OpenXML工具创建word和excel文件的字节数组,并使用ZipArchive压缩它们并返回filebyte httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK); httpResponseMessage.Content = new ByteArrayContent(zipFileBytes); httpResponseMessage.Content.Headers.ContentDisposition = n

我使用OpenXML工具创建word和excel文件的字节数组,并使用ZipArchive压缩它们并返回filebyte

httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK);
httpResponseMessage.Content = new ByteArrayContent(zipFileBytes);
httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/zip");
httpResponseMessage.Content.Headers.ContentLength = zipFileBytes.Length;
httpResponseMessage.Content.Headers.Add("xfilename", zipFileName);
httpResponseMessage.StatusCode = HttpStatusCode.OK;
httpResponseMessage.Content.Headers.Add("Access-Control-Expose-Headers", "xfilename");

return httpResponseMessage;
解压zip文件后,可以下载并打开该文件

但是,它不能被窗口资源管理器或其他解压软件审查。 尝试在窗口资源管理器中打开文档时,出现错误消息

“Windows无法完成提取。目标文件可能会丢失。” “不可创建”

关于如何解决这个问题有什么想法吗?文档是否可以在由OpenXML创建的zip中进行查看

更新: 我正在使用“OpenXMLSDK2.5生产力工具”生成代码。下面的代码是生成文档的代码。(有关详细信息,请使用该工具生成代码,因为代码行太多了)

使用DocumentFormat.OpenXml.Packaging;
使用Ap=DocumentFormat.OpenXml.ExtendedProperties;
使用DocumentFormat.OpenXml.Wordprocessing;
使用DocumentFormat.OpenXml;
使用M=DocumentFormat.OpenXml.Math;
使用Ovml=DocumentFormat.OpenXml.Vml.Office;
使用V=DocumentFormat.OpenXml.Vml;
使用W15=DocumentFormat.OpenXml.Office2013.Word;
使用A=DocumentFormat.OpenXml.Drawing;
使用Thm15=DocumentFormat.OpenXml.Office2013.Theme;
命名空间生成代码
{
公共类生成类
{
//创建WordprocessingDocument。
public void CreatePackage(数据模型数据模型,列表数据列表,输出字符串文件名,输出字节[]文件字节)
{
filename=string.Empty;
fileBytes=null;
使用(MemoryStream ms=new MemoryStream())
{
尝试
{
使用(WordprocessingDocument包=WordprocessingDocument.Create(ms,WordprocessingDocumentType.Document))
{
CreateParts(包、数据模型、数据列表);
}
字符串扩展名=“.docx”;
filename=“TestDoc”+扩展名;
fileBytes=ms.ToArray();
Close女士();
返回;
}
捕获(系统异常)
{
投掷;
}
}
}
}
然后,我使用下面的代码生成zip文件,并从CreatePackage函数传递数组字节列表

public byte[] zipByteDocument(List<Tuple<Byte[], string>> fileBytes)
{
    // the output bytes of the zip
    byte[] zipFileBytes = null;

    // create a working memory stream
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
    {
        // create a zip
        using (System.IO.Compression.ZipArchive zip = new System.IO.Compression.ZipArchive(memoryStream, System.IO.Compression.ZipArchiveMode.Create, true))
        {
            // interate through the source files
            foreach (Tuple<Byte[], string> file in fileBytes)
            {
                // add the item name to the zip
                System.IO.Compression.ZipArchiveEntry zipItem = zip.CreateEntry(file.Item2);
                // add the item bytes to the zip entry by opening the original file and copying the bytes 
                using (System.IO.MemoryStream originalFileMemoryStream = new System.IO.MemoryStream(file.Item1))
                {
                    using (System.IO.Stream entryStream = zipItem.Open())
                    {
                        originalFileMemoryStream.CopyTo(entryStream);
                    }
                }
            }
        }
        zipFileBytes = memoryStream.ToArray();
    }
    return zipFileBytes;
}
public byte[]ZipByte文档(列出文件字节)
{
//zip文件的输出字节数
字节[]zipFileBytes=null;
//创建工作内存流
使用(System.IO.MemoryStream MemoryStream=new System.IO.MemoryStream())
{
//创建一个拉链
使用(System.IO.Compression.ZipArchive zip=new System.IO.Compression.ZipArchive(memoryStream,System.IO.Compression.ZipArchiveMode.Create,true))
{
//通过源文件进行交互
foreach(以fileBytes为单位的元组文件)
{
//将项目名称添加到zip
System.IO.Compression.ZipArchiveEntry zipItem=zip.CreateEntry(file.Item2);
//通过打开原始文件并复制字节,将项目字节添加到zip条目中
使用(System.IO.MemoryStream originalFileMemoryStream=new System.IO.MemoryStream(file.Item1))
{
使用(System.IO.Stream entryStream=zipItem.Open())
{
originalFileMemoryStream.CopyTo(入口流);
}
}
}
}
zipFileBytes=memoryStream.ToArray();
}
返回zipFileBytes;
}

最后,我将zipFileBytes传递给httpResponseMessage,它可以下载。但是,如果不解压缩zip文件,就无法预览它。

我创建了一些单元测试(见下文),这些测试表明您共享的代码不应该有问题(但是,请注意,我没有简单地复制您的代码)。可能是生成的代码或您未共享的其他代码造成的

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用系统IO压缩;
Net系统;
使用System.Net.Http;
使用System.Net.Http.Header;
使用System.Threading.Tasks;
使用CodeSnippets.IO;
使用Xunit;
命名空间CodeSnippets.Tests.IO.Compression
{
公共类ZipArchiveTests
{
私有静态字节[]CreateZipArchiveBytes(IEnumerable文件)
{
使用MemoryStream stream=CreateZipArchiveStream(文件);
返回流ToArray();
}
私有静态内存流CreateZipArchiveStream(IEnumerable文件)
{
var stream=newmemoryStream();
使用(CreateZipArchive(流、文件))
回流;
}
私有静态ZipArchive CreateZipArchive(流、IEnumerable文件)
{
如果(stream==null)抛出新的ArgumentNullException(nameof(stream));
如果(files==null)抛出新的ArgumentNullException(nameof(files));
var archive=new ZipArchive(stream,ZipArchiveMode.Create,true);
文件中的foreach((字节[]文件内容,字符串文件名)
{
ZipArchiveEntry archiveEntry=archive.CreateEntry(文件名);
使用Stream entryStream=archiveEntry.Open();
Write(fileContent,0,fileContent.Length);
}
归还档案;
}
专用静态ZipArchive ReadZipArchive(字节[]zipArchiveBytes)
{
返回新的ZipArchive(新内存流(zipArchiveBytes),ZipArchiveMode.Read,false);
}
私有静态字节[]ReadEntryBytes(ZipArchive ZipArchive,string entryName)
{
ZipArchiveEntry entry=zipArchive.GetEntry(entryName)??抛出新异常();
var entryBytes=新字节[entry.Length];
使用Stream entryStream=entry.Open();
entryStream.Read(en
public byte[] zipByteDocument(List<Tuple<Byte[], string>> fileBytes)
{
    // the output bytes of the zip
    byte[] zipFileBytes = null;

    // create a working memory stream
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
    {
        // create a zip
        using (System.IO.Compression.ZipArchive zip = new System.IO.Compression.ZipArchive(memoryStream, System.IO.Compression.ZipArchiveMode.Create, true))
        {
            // interate through the source files
            foreach (Tuple<Byte[], string> file in fileBytes)
            {
                // add the item name to the zip
                System.IO.Compression.ZipArchiveEntry zipItem = zip.CreateEntry(file.Item2);
                // add the item bytes to the zip entry by opening the original file and copying the bytes 
                using (System.IO.MemoryStream originalFileMemoryStream = new System.IO.MemoryStream(file.Item1))
                {
                    using (System.IO.Stream entryStream = zipItem.Open())
                    {
                        originalFileMemoryStream.CopyTo(entryStream);
                    }
                }
            }
        }
        zipFileBytes = memoryStream.ToArray();
    }
    return zipFileBytes;
}