【问题标题】:Can't seem to zip up xml file correctly using C#似乎无法使用 C# 正确压缩 xml 文件
【发布时间】:2018-01-18 00:07:19
【问题描述】:

好的,所以我在网上尝试了很多示例,但我似乎无法正常工作。

我有以下代码创建一个 zip 存档,当我提取 zip 存档时有一个 xml 文件。

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("========================");
                        }
                    }
                }

现在的问题是,如果我打开该 xml 文件,则原始 xml 文件中的数据不存在。所以不要看到这样的东西:

<?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>

我明白了:

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

我认为这与将文件作为流打开有关,但由于我对 C# 还很陌生,所以无法弄清楚。

我也试过了:

writer.Write(bytes);

谁能帮忙,干杯。

【问题讨论】:

    标签: c# xml compression nopcommerce


    【解决方案1】:

    我明白了:

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

    这就是你告诉它的:

    writer.Write(oldFilePath);
    

    也许是:

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

    【讨论】:

    • 感谢这确实有帮助,但我必须使用它才能使其正常工作source.CopyTo(writer.BaseStream);
    • @chrisc 再看看我的代码:我什至没有 writer...这不是一个错误;这里的writer 完全没必要
    • 啊,是的,我看到你在这里做了什么。很有魅力,谢谢 m8 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多