[Content_Types].xml folder different make System.IO.Packaging.Package can't read Uri information.
My [Content_Types].xml use extension search :
<?xml version="1.0" encoding="utf-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="xml" ContentType="" />
</Types>
But, I just add a folder before style.xml
then System.IO.Packaging.Package
can't get Uri
information
My Code:
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);
}
}
}
I got the key point : it should use /
not \
e.g:
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);
}
}
}