/// <summary> /// Load a XamlPackage into Memory /// </summary> /// <param name="filename">Suggested Extension: .xpkg</param> /// <returns></returns> public static Package Load(string filename) { var ret = new Package(); using(var z = new ZipFile(filename)) { var metaSr = new StreamReader((from e in z.Entries where e.FileName == "meta.xaml" select e).ToArray()[0].OpenReader()); ret.Meta = (Dictionary<string, object>)XamlServices.Parse(metaSr.ReadToEnd()); var contentEntries = (from e in z.Entries where (e.IsDirectory == false && e.FileName.StartsWith("content/")) select e).ToArray(); foreach (var item in contentEntries) { var sr = new StreamReader(item.OpenReader()); ret.AddXaml(item.FileName.Substring("content/".Length), sr.ReadToEnd()); } } return ret; }
/// <summary> /// Save a Package in to Filesystem /// </summary> /// <param name="filename">Suggested Extension: .xpkg</param> /// <param name="pkg">The saving Package</param> public static void Save(string filename, Package pkg) { using(var z = new ZipFile()) { var content = z.AddDirectoryByName("content"); var res = z.AddDirectoryByName("res"); foreach (var r in pkg.XamlFiles) { z.AddEntry($"content/{r.Key}", r.Value); } foreach (var r in pkg.Ressources) { z.AddEntry($"res/{r.Key}", r.Value); } z.AddEntry("meta.xaml", XamlServices.Save(pkg.Meta)); z.Save(filename); } }