Search code examples
c#xmlcompressionnopcommerce

Can't seem to zip up xml file correctly using C#


Ok so I have tried numerous examples on the web, but I can't seem to get any working.

I have the following code which creates a zip archive, and when I extract the zip archive there is a xml file.

string filePathZip = Path.Combine(HttpRuntime.AppDomainAppPath, "content\\files\\exportimport",
                prependStoreId + "ebayproductinventorysyncfeed.zip");

                using (FileStream zipToOpen = new FileStream(filePathZip, (!File.Exists(filePathZip)) ? FileMode.CreateNew : FileMode.Open))
                {
                    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                    {
                        //byte[] bytes = File.ReadAllBytes(oldFilePath);
                        ZipArchiveEntry readmeEntry = archive.CreateEntry(oldFilePath);
                        using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                        {
                            writer.Write(oldFilePath);
                            Debug.WriteLine("Information about this package.");
                            Debug.WriteLine("========================");
                        }
                    }
                }

Now the problem is if I open that xml file the data from the original xml file is not there. So instead of seeing something like this:

<?xml version="1.0" encoding="utf-8"?>
<ReviseInventoryStatusRequest xmlns="urn:ebay:apis:eBLBaseComponents">
  <RequesterCredentials>
    <eBayAuthToken></eBayAuthToken>
  </RequesterCredentials>
  <Version>967</Version>
  <ErrorLanguage>en_US</ErrorLanguage>
  <WarningLevel>High</WarningLevel>
</ReviseInventoryStatusRequest>

I get this:

C:\projects\website\Presentation\Nop.Web\content\files\exportimport\ebaylmsinventoryfeed.xml

I figure it's something to do with opening the file as a stream, just can't figure it out as I'm fairly new to C#.

I have also tried:

writer.Write(bytes);

Can anyone help, cheers.


Solution

  • I get this:

    C:\projects\website\Presentation\Nop.Web\content\files\exportimport\ebaylmsinventoryfeed.xml

    That's what you told it to do:

    writer.Write(oldFilePath);
    

    Perhaps instead:

    using (var dest = readmeEntry.Open()))
    using (var source = File.OpenRead(oldFilePath))
    {
        source.CopyTo(dest);
    }