1
    public ActionResult CreateArchivePackage(GetObjectResponse response)
    {
        using (var memoryStream = new MemoryStream())
        {
            response?.ResponseStream.CopyTo(memoryStream);

            using (System.IO.Compression.ZipArchive zip = new System.IO.Compression.ZipArchive(memoryStream, System.IO.Compression.ZipArchiveMode.Create, true))
            {
                ZipArchiveEntry zipItem = zip.CreateEntry("Asmosis" + ".xml");
                using (MemoryStream originalFileMemoryStream = new MemoryStream(Encoding.Default.GetBytes(CreateXMLDocForNow().OuterXml)))
                {
                    using (System.IO.Stream entryStream = zipItem.Open())
                    {
                        originalFileMemoryStream.CopyTo(entryStream);
                        return File(originalFileMemoryStream.ToArray(), "random/zip");
                    }
                }
            }
        }
    }

I have tried several different approaches such as converting the xml file into a stream and then adding that to the current memory stream with no luck! Any help would be greatly appreciated, this is my first time working with the streams, files, etc.

DerMann
  • 11
  • 2
  • "// Not working" what do you mean by that? does it gives empty result? Exception? other? – Pac0 Apr 02 '20 at 15:08
  • @Pac0, the original zip "response" I pass to the function is returned just fine, when I add the new xml file, it is no where to be found. – DerMann Apr 02 '20 at 15:13
  • Just to be clear. I don't think you are adding the xml to the zip. I'm pretty sure this would just be trying to make a hybrid zip/xml stream which doesn't exist. – Patrick Mcvay Apr 02 '20 at 15:21
  • I am almost certain that you would have to provide a mechanism for decompressing the zip, then add the xml document to the decompressed zip, then recompressing the zip. – Patrick Mcvay Apr 02 '20 at 15:25
  • @PatrickMcvay what approach would you take? I simply need to add a xml file created on the fly to a zip file that I receive on my end as a ResponseStream. – DerMann Apr 02 '20 at 15:25
  • I think this question is strongly related to problem : https://stackoverflow.com/questions/2266204/creating-zip-file-from-stream-and-downloading-it (a bit old, though) – Pac0 Apr 02 '20 at 15:53
  • It almost seems like adding your on-the-fly xml file to the temporary files location on your server would work best. You could then use the ZipArchive.CreateEntryFromFile() method, but honestly I have never done anything with zip archives. So, I could be completely wrong. – Patrick Mcvay Apr 02 '20 at 15:54
  • I agree with @Pac0 – Patrick Mcvay Apr 02 '20 at 15:56

0 Answers0