Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic Zip : Zip file creation from byte[]

Tags:

c#

dotnetzip

Ionic zip allows me to add existing file to zip object and create a zip file. But considering that I am reading those byte[] from created zip file and sending over server, I need to again create zip file from that byte[] to store zip on server. How do I achieve this ?

I am using C#.

like image 292
Cannon Avatar asked Mar 24 '12 21:03

Cannon


2 Answers

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");
    }
}
like image 74
hangy Avatar answered Nov 20 '22 06:11

hangy


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"...
like image 10
Jon Skeet Avatar answered Nov 20 '22 08:11

Jon Skeet