title: System.IO: Convert zip file to byte[] without saving file
body: <p>I'm having trouble using the System.IO.Compression library to create a zip file and then return the byte[] for that zip file. So I want the method to take an input string--the path for the file to be zipped--and return the corresponding byte array for the file once it's been zipped. Currently, I can save the zip file to some known path and then use System.IO.File.ReadAllBytes(path) to get the byte[].</p>
  
  <p>But how can I do this if I don't know beforehand where that zip file should end up (if anywhere at all)? My attempt at the solution is below. It doesn't seem to be working because when I try to reconstruct the zip file from the byte array it says that the archive is in an unknown format or damaged. Note: I've been able to do this using the Ionic DotNetZip library; however, I'm trying to do the same with only the use of System.IO libraries.</p>
  
  <p>Any ideas on how to go about this?</p>
  
  <pre><code>private static byte[] CreateZipAndFindBytes(string importfile)
  {
    byte[] retVal = null;
  
    using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
    {
      //ZipArchive(Stream, ZipArchiveMode, Boolean) //true means leave stream open
      using (System.IO.Compression.ZipArchive archive = new System.IO.Compression.ZipArchive(memStream, System.IO.Compression.ZipArchiveMode.Create, true))
      {
        archive.CreateEntryFromFile(importfile, "test.zip"); //adds the file to the zip
      }
      retVal = memStream.ToArray();
    }
    return retVal;
  }
  </code></pre>