C#创建包含多个Word文档的Zip

本文关键字:文档 Zip Word 创建 包含多 | 更新日期: 2023-09-27 18:26:22

当前被一个损坏的.zip文件夹卡住,该文件夹包含我认为正确的内容(随着包含更多word文档,大小会增加)。

不确定它为什么会破坏zip——它生成的word文档很好,因为这是我以前的实现,而且没有改变。

private void GenReport(List<PerformanceReport> performanceReportData2, string start, string end)
        {
            List<byte[]> byteList = new List<byte[]>();
            var document = Server.MapPath("~/Content/docs/Performance Report.docx");
            var byteArray = System.IO.File.ReadAllBytes(document);
            // Format parameters
            var startRange = DateTime.Parse(start);
            var endRange = DateTime.Parse(end);
            foreach (var performanceReportData in performanceReportData2)
            {
                var trust = performanceReportData.Name.Replace(",", string.Empty);
                trust = trust.Replace("&", string.Empty);
                start = start.Replace("/", "-");
                end = end.Replace("/", "-");
                using (var stream = new MemoryStream())
                {
                    stream.Write(byteArray, 0, byteArray.Length);
                    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true))
                    {
                        var body = wordDoc.MainDocumentPart;
                        ....... Do stuff
                        body.Document.Save();
                        byteList.Add(stream.ToArray());
                    }
                }
            }
            using (var ms = new MemoryStream())
            {
                using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
                {
                    foreach (var attachment in byteList)
                    {
                        var entry = zipArchive.CreateEntry("test.docx");
                        using (var originalFile = new MemoryStream(attachment))
                        {
                            using (var zipEntryStream = entry.Open())
                            {
                                originalFile.CopyTo(zipEntryStream);
                            }
                        }
                    }
                    Response.ContentType = "application/zip";
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + "Performance Report.zip");
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.CopyTo(Response.OutputStream);
                    Response.End();
                }
            }
        }

我已经想不出为什么它会损坏Zip文件夹了——如果我在文本编辑器中打开Zip文件夹,它会以PK开始,我收集到PK确认它是Zip。

C#创建包含多个Word文档的Zip

我的作用域不正确。

工作代码看起来像这个

            using (var ms = new MemoryStream())
            {
                using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
                {
                    foreach (var attachment in byteList)
                    {
                        var entry = zipArchive.CreateEntry(attachment.Key);
                        using (var originalFile = new MemoryStream(attachment.Value))
                        {
                            using (var zipEntryStream = entry.Open())
                            {
                                originalFile.CopyTo(zipEntryStream);
                            }
                        }
                    }
                }
                Response.ContentType = "application/zip";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + "Reports.zip");
                ms.Seek(0, SeekOrigin.Begin);
                ms.CopyTo(Response.OutputStream);
                Response.End();