Ionic Zip : Zip file creation from byte[]

馋奶兔 提交于 2019-12-03 14:06:46

If I understand your question correctly, you get your byte[] data array over the network and want to save that data in a zip file? You can create a new ZipEntry from a MemoryStream which you create from the byte[] you got (as shown in the docs):

byte[] data = MethodThatReceivesYourDataOverTheNet();
using (MemoryStream stream = new MemoryStream(data))
{
    using (ZipFile zip = new ZipFile())
    {
        zip.AddEntry("name_of_the_file_in_the_arhive.bin", "base", stream);
        zip.Save("example.zip");
    }
}

It's not really clear from your question what you're doing - but if you're just trying to avoid saving to disk and then reloading to get the data, just save to a MemoryStream:

byte[] data;
using (MemoryStream ms = new MemoryStream())
{
    zipFile.Save(ms);
    data = ms.ToArray();
}
// Do whatever with data.

Alternatively, use MemoryStream.GetBuffer() to avoid making another copy:

byte[] buffer;
int length;
using (MemoryStream ms = new MemoryStream())
{
    zipFile.Save(ms);
    buffer = ms.ToArray();
    length = ms.Length;
}

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