Convert DotNetZip ZipFile to byte array

送分小仙女□ 提交于 2019-12-10 16:27:17

问题


I've built a DotNetZip ZipFile with several entries. I'd like to convert it to a byte array so I can download it using the download construct below.

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

I recognize that there is a ZipFile method for this:

wrkZip.Save(context.Response.OutputStream)

However, I've got a difficult bug in using that, described here:

DotNetZip download works in one site, not another

so I'm looking for a short term workaround. The short story on the bug is that the the ZipFile writes to disk fine, and downloads fine in a very similar website; it just doesn't work in the case I need it to right now.

So, how to convert a DotNetZip ZipFile to a byte array? I've looked at other answers however they don't describe this particular case of converting a whole, loaded ZipFile.


回答1:


Use a MemoryStream to get the contents into a byte array:

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


来源:https://stackoverflow.com/questions/30928625/convert-dotnetzip-zipfile-to-byte-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!