ZipFile to byte[] without saving.

Oct 22, 2009 at 4:43 PM

Hi

I have this:

 

ZipFile zf = new ZipFile();
            zf.Encryption = EncryptionAlgorithm.WinZipAes128;
            zf.CompressionLevel = CompressionLevel.BestCompression;
            zf.AddFile(srcfileName);

Here without saving this file to hard disk i would like to get the byte[] of zip file.

Just wondering if it is possible.

Thanks

N

Coordinator
Oct 22, 2009 at 6:48 PM

Yes, you should save the ZipFile to a memorystream (use ZipFile.Save(Stream) method) , and then use the MemoryStream.ToArray() method to get the byte array.

using (var zip = new Ionic.Zip.ZipFile())
{
    zip.CompressionLevel= Ionic.Zlib.CompressionLevel.BestCompression;
    zip.Password = "VerySecret.";
    zip.Encryption = EncryptionAlgorithm.WinZipAes128;
    zip.AddFile(sourceFileName);
    MemoryStream output = new MemoryStream();
    zip.Save(output);

    byte[] zipbytes = output.ToArray();
}

 

Oct 23, 2009 at 5:27 AM

Thanks Cheeso.