Writing to ZipArchive using the HttpContext OutputStream

前端 未结 5 1082
小鲜肉
小鲜肉 2020-12-01 10:05

I\'ve been trying to get the \"new\" ZipArchive included in .NET 4.5 (System.IO.Compression.ZipArchive) to work in a ASP.NET site. But it seems like it doesn\'t

5条回答
  •  旧时难觅i
    2020-12-01 11:04

    An simplified version of svick's answer for zipping a server-side file and sending it via the OutputStream:

    using (var outputStream = new PositionWrapperStream(Response.OutputStream))
    using (var archive = new ZipArchive(outputStream, ZipArchiveMode.Create, false))
    {
        var entry = archive.CreateEntryFromFile(fullPathOfFileOnDisk, fileNameAppearingInZipArchive);
    }
    

    (In case this seems obvious, it wasn't to me!)

提交回复
热议问题