c# - 从内存流创建 Zip 文件 C#

标签 c# zip memorystream

基本上,用户应该能够单击一个链接并下载多个 pdf 文件。但问题是我无法在服务器或任何地方创建文件。一切都必须在内存中。

我能够创建内存流并将其 Response.Flush() 作为 pdf 文件,但如何在不创建文件的情况下压缩多个内存流。

这是我的代码:

Response.ContentType = "application/zip";

// If the browser is receiving a mangled zipfile, IIS Compression may cause this problem. Some members have found that
// Response.ContentType = "application/octet-stream" has solved this. May be specific to Internet Explorer.
Response.AppendHeader("content-disposition", "attachment; filename=\"Download.zip\"");
Response.CacheControl = "Private";
Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); // or put a timestamp in the filename in the content-disposition                

byte[] abyBuffer = new byte[4096];

ZipOutputStream outStream = new ZipOutputStream(Response.OutputStream);
outStream.SetLevel(3);

#region Repeat for each Memory Stream
MemoryStream fStream = CreateClassroomRoster();// This returns a memory stream with pdf document

ZipEntry objZipEntry = new ZipEntry(ZipEntry.CleanName("ClassroomRoster.pdf"));
objZipEntry.DateTime = DateTime.Now;
objZipEntry.Size = fStream.Length;
outStream.PutNextEntry(objZipEntry);

int count = fStream.Read(abyBuffer, 0, abyBuffer.Length);
while (count > 0)
{
    outStream.Write(abyBuffer, 0, count);
    count = fStream.Read(abyBuffer, 0, abyBuffer.Length);
    if (!Response.IsClientConnected)
        break;

    Response.Flush();
}

fStream.Close();

#endregion

outStream.Finish();
outStream.Close();

Response.Flush();
Response.End();

这会创建一个 zip 文件,但里面没有文件

我正在使用 使用 iTextSharp.text - 创建 pdf 使用 ICSharpCode.SharpZipLib.Zip - 用于压缩

谢谢, 卡维塔

最佳答案

此链接介绍了如何使用 SharpZipLib 从 MemoryStream 创建 zip:https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples#wiki-anchorMemory 。使用它和 iTextSharp,我能够压缩在内存中创建的多个 PDF 文件。

这是我的代码:

MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);

zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
byte[] bytes = null;

// loops through the PDFs I need to create
foreach (var record in records)
{
    var newEntry = new ZipEntry("test" + i + ".pdf");
    newEntry.DateTime = DateTime.Now;

    zipStream.PutNextEntry(newEntry);

    bytes = CreatePDF(++i);

    MemoryStream inStream = new MemoryStream(bytes);
    StreamUtils.Copy(inStream, zipStream, new byte[4096]);
    inStream.Close();
    zipStream.CloseEntry();
}

zipStream.IsStreamOwner = false;    // False stops the Close also Closing the underlying stream.
zipStream.Close();          // Must finish the ZipOutputStream before using outputMemStream.

outputMemStream.Position = 0;

return File(outputMemStream.ToArray(), "application/octet-stream", "reports.zip");

CreatePDF 方法:

private static byte[] CreatePDF(int i)
{
    byte[] bytes = null;
    using (MemoryStream ms = new MemoryStream())
    {
        Document document = new Document(PageSize.A4, 25, 25, 30, 30);
        PdfWriter writer = PdfWriter.GetInstance(document, ms);
        document.Open();
        document.Add(new Paragraph("Hello World " + i));
        document.Close();
        writer.Close();
        bytes = ms.ToArray();
    }

    return bytes;
}

关于c# - 从内存流创建 Zip 文件 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10012178/

相关文章:

c# - 如何正确创建 ZipArchive?

c# - MemoryStream.WriteTo(Stream destinationStream) 与 Stream.CopyTo(Stream destinationStream)

c# - 来自 MemoryStream UTF8 编码的 StreamReader

c# - 使用 ASP.NET Core Entity Framework 在数据层中进行数据加密

c# - 给定编码中的 xml 无效字符

c# - 删除 XAML 用户控件上的 DataContextChanged/Loaded 事件处理程序

.net - 如何修改内存中的 MSI?

c# - 如何从客户端验证中删除模型的属性?

java - 维护 zip 文件中的 Windows 目录

java - 将 zip 文件从服务器下载到设备时出错?