【问题标题】:Convert DotNetZip ZipFile to byte array将 DotNetZip ZipFile 转换为字节数组
【发布时间】:2015-09-04 20:59:55
【问题描述】:

我已经构建了一个包含多个条目的 DotNetZip ZipFile。我想将它转换为字节数组,以便我可以使用下面的下载结构下载它。

   Using wrkZip As New ZipFile
        '----- create zip, add memory stream----------
       For n As Integer = 0 To wrkAr.Count - 1
           wrkFS = wrkAr(n)
           wrkZip.AddEntry(wrkFS.FileName, wrkFS.ContentStream)
       Next

   dim wrkBytes() as Byte
   dim wrkFileName as string = "Test.txt"

   ===> wrkBytes = ConvertToByteArray(wrkZip) <==== 

    context.Response.Clear()
        context.Response.ContentType = "application/force-download"
        context.Response.AddHeader("content-disposition", "attachment; filename=" & wrkFileName)
        context.Response.BinaryWrite(wrkBytes)
        wrkBytesInStream = Nothing
        context.Response.End()

我知道有一个 ZipFile 方法可以解决这个问题:

wrkZip.Save(context.Response.OutputStream)

但是,我在使用它时遇到了一个困难的错误,如下所述:

DotNetZip download works in one site, not another

所以我正在寻找一个短期的解决方法。关于该错误的简短故事是 ZipFile 可以正常写入磁盘,并且可以在非常相似的网站上正常下载;它只是在我现在需要它的情况下不起作用。

那么,如何将 DotNetZip ZipFile 转换为字节数组?我查看了其他答案,但是他们没有描述转换整个加载的 ZipFile 的这种特殊情况。

【问题讨论】:

    标签: vb.net dotnetzip


    【解决方案1】:

    使用MemoryStream 将内容放入字节数组中:

    Dim ms as New MemoryStream
    wrkZip.Save(ms)
    wrkBytes = ms.ToArray()
    

    【讨论】:

    • 那行得通。没有解决下载问题,但回答了问题,并消除了 ZipFile.Save(to page context) 作为下载问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多