Returning a zip file as a Byte[] in C# ??

Mar 1, 2011 at 4:09 PM

The following routine will read an ArrayList of Byte[] and load each entry into the zip file.
When I use zip.save(somefilename) it saves my zip file to my disk and looks perfect.


But, now I need to return a byte[] from this method which contains the zip file.

How would I do this?

Any help will be greatly appreciated.

private byte[] GenerateMergedZipFile(ArrayList files, string[] byteInfo, string finalZipFormat, string ZipFileName)
{
 byte[] result = null;

               
 using (ZipFile zip = new ZipFile())
 {
  int ctr = 0;
         foreach (Byte[] inputFile in files)
         {
          // Add the stream into the zip entry.
                    ZipEntry e = zip.AddEntry(byteInfo[ctr] + "." + finalZipFormat, inputFile);
                    ctr++;
         }


                // Save the zip file.

                zip.Save ???
  result =??
                return result;
  }
}

Coordinator
Mar 9, 2011 at 1:10 PM

If what you want is an array of bytes containing the data for the zipfile, I suggest passing a MemoryStream to your call to ZipFile.Save(), and then calling the ToArray() method on the MemoryStream. There's no need to save the zipfile to a filesystem file.

If you want BOTH a filesystem file and a byte array, then save to a filesystem file, and then read the data from the filesystem file using the appropriate method on the System.IO.File class.  Or, you could do the converse - save to a MemoryStream, then write the data into the filesystem file using the appropriate method on the System.IO.File class. 

Good luck.

 

Mar 9, 2011 at 1:11 PM

Thanks for your response.

From: Cheeso [email removed]
Sent: Wednesday, March 09, 2011 9:10 AM
To: Chernick, Howie
Subject: Re: Returning a zip file as a Byte[] in C# ?? [DotNetZip:248017]

From: Cheeso

If what you want is an array of bytes containing the data for the zipfile, I suggest passing a MemoryStream to your call to ZipFile.Save(), and then calling the ToArray() method on the MemoryStream. There's no need to save the zipfile to a filesystem file.

If you want BOTH a filesystem file and a byte array, then save to a filesystem file, and then read the data from the filesystem file using the appropriate method on the System.IO.File class. Or, you could do the converse - save to a MemoryStream, then write the data into the filesystem file using the appropriate method on the System.IO.File class.

Good luck.

 
--------------------------------------------------
This e-mail and any files transmitted with it may contain privileged or confidential information.
It is solely for use by the individual for whom it is intended, even if addressed incorrectly.
If you received this e-mail in error, please notify the sender; do not disclose, copy, distribute,
or take any action in reliance on the contents of this information; and delete it from
your system. Any other use of this e-mail is prohibited.
 
Thank you for your compliance.
--------------------------------------------------