【问题标题】:C# Creating Zip with multiple Word Docs insideC# 创建包含多个 Word 文档的 Zip
【发布时间】:2016-03-16 07:26:58
【问题描述】:

目前卡在一个损坏的 .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 开头,我收集到确认它是一个 Zip。

【问题讨论】:

    标签: c# file download stream zip


    【解决方案1】:

    我的范围不正确。

    工作代码如下所示

                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();
    

    【讨论】:

      猜你喜欢
      • 2015-01-21
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多