public void InitFrom(Stream templateStream) { if (templateStream == null) { throw new ArgumentNullException("templateStream"); } this.file = null; this.name = "Unknown"; this.specialTag = SpecialInfo.None; using (ZipArchive templateZip = new ZipArchive(templateStream)) { ZipArchiveEntry entryInfo = templateZip.Entries.FirstOrDefault(z => z.Name == "TemplateInfo.xml"); ZipArchiveEntry entryIcon = templateZip.Entries.FirstOrDefault(z => z.Name == "TemplateIcon.png"); if (entryIcon != null) { using (MemoryStream str = new MemoryStream()) { entryIcon.Extract(str); str.Seek(0, SeekOrigin.Begin); this.icon = new Bitmap(str); } } if (entryInfo != null) { string xmlSource = null; using (MemoryStream str = new MemoryStream()) { entryInfo.Extract(str); str.Seek(0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader(str)) { xmlSource = reader.ReadToEnd(); } } XDocument xmlDoc = XDocument.Parse(xmlSource); XElement elemName = xmlDoc.Element("name"); if (elemName != null) { this.name = elemName.Value; } XElement elemDesc = xmlDoc.Element("description"); if (elemDesc != null) { this.desc = elemDesc.Value; } } } return; }
public static void Extract(this ZipArchiveEntry entry, string targetPath, bool overwrite) { if (!overwrite && File.Exists(targetPath)) { return; } using (Stream stream = File.Open(targetPath, FileMode.Create, FileAccess.Write, FileShare.None)) { entry.Extract(stream); } }