[Content_Types].xml文件夹不同使System.IO.Packaging.Package无法读取Uri信息

[Content_Types].xml folder different make System.IO.Packaging.Package can't read Uri information

[Content_Types].xml文件夹不同使System.IO.Packaging.Package无法读取Uri信息。

我的 [Content_Types].xml 使用扩展搜索:

<?xml version="1.0" encoding="utf-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="xml" ContentType="" />
</Types>

但是,我只是在style.xml之前添加了一个文件夹,然后System.IO.Packaging.Package无法获取Uri信息

我的代码:

namespace ZipSample
{
    using System;
    using System.IO;
    using System.IO.Compression;
    using System.IO.Packaging;
    using System.Linq;
    using System.Text;
    public class Program
    {
        public static void Main(string[] args)
        {
            Execute(xmlpath:@"styles.xml");
            Execute(xmlpath:@"test\styles.xml");
        }
        
        public static void Execute(string xmlpath)
        {
            var path = "output.zip";
            File.Delete(path);
            using (var stream = new FileStream(path,FileMode.CreateNew,FileAccess.ReadWrite))
            {
                using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, UTF8Encoding.UTF8))
                {
                    AddStringToZip(archive, xmlpath, @"");
                    AddStringToZip(archive,
                        @"[Content_Types].xml", @"<?xml version=""1.0"" encoding=""utf-8""?>
<Types xmlns=""http://schemas.openxmlformats.org/package/2006/content-types"">
    <Default Extension=""xml"" ContentType="""" />
</Types>");
                }
            }

            using (Package zip = System.IO.Packaging.Package.Open(path))
            {
                var part = zip.GetParts().Select(s => new { s.CompressionOption, s.ContentType, s.Uri, s.Package.GetType().Name }).FirstOrDefault();
                Console.WriteLine($"xmlpath:{xmlpath}  |  CompressionOption:{part?.CompressionOption} |  Uri:{part?.Uri}");
            }
        }

        private static void AddStringToZip(ZipArchive archive, string entryPath, string content)
        {
            var utf8WithBom = new System.Text.UTF8Encoding(true);
            ZipArchiveEntry entry = archive.CreateEntry(entryPath);
            using (var stream = entry.Open())
            using (StreamWriter writer = new StreamWriter(stream, utf8WithBom))
                writer.Write(content);
        }
    }
}

我明白了关键点:它应该使用 / 而不是 \

例如:

namespace ZipSample
{
    using System;
    using System.IO;
    using System.IO.Compression;
    using System.IO.Packaging;
    using System.Linq;
    using System.Text;
    public class Program
    {
        public static void Main(string[] args)
        {
            Execute(xmlpath:@"styles.xml");
            Execute(xmlpath:@"test/styles.xml");
        }
        
        public static void Execute(string xmlpath)
        {
            var path = "output.zip";
            File.Delete(path);
            using (var stream = new FileStream(path,FileMode.CreateNew,FileAccess.ReadWrite))
            {
                using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, UTF8Encoding.UTF8))
                {
                    AddStringToZip(archive, xmlpath, @"");
                    AddStringToZip(archive,
                        @"[Content_Types].xml", @"<?xml version=""1.0"" encoding=""utf-8""?>
<Types xmlns=""http://schemas.openxmlformats.org/package/2006/content-types"">
    <Default Extension=""xml"" ContentType="""" />
</Types>");
                }
            }

            using (Package zip = System.IO.Packaging.Package.Open(path))
            {
                var part = zip.GetParts().Select(s => new { s.CompressionOption, s.ContentType, s.Uri, s.Package.GetType().Name }).FirstOrDefault();
                Console.WriteLine($"xmlpath:{xmlpath}  |  CompressionOption:{part?.CompressionOption} |  Uri:{part?.Uri}");
            }
        }

        private static void AddStringToZip(ZipArchive archive, string entryPath, string content)
        {
            var utf8WithBom = new System.Text.UTF8Encoding(true);
            ZipArchiveEntry entry = archive.CreateEntry(entryPath);
            using (var stream = entry.Open())
            using (StreamWriter writer = new StreamWriter(stream, utf8WithBom))
                writer.Write(content);
        }
    }
}