将 DotNetZip ZipFile 转换为字节数组 [英] Convert DotNetZip ZipFile to byte array

查看:17
本文介绍了将 DotNetZip ZipFile 转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我知道有一个用于此的 ZipFile 方法:

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 下载适用于一个网站,而不适用于另一个

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

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.

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

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.

推荐答案

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

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

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

这篇关于将 DotNetZip ZipFile 转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆