private static void ProcessFolderNode(XmlNode node, string basePath, bool overwrite) { string folderName = node.Attributes["name"].Value; string newPath = Path.Combine(basePath, folderName); DirectoryInfo di = new DirectoryInfo(newPath); if (!di.Exists) { di.Create(); } else if (!overwrite) { throw new Exception("Directory " + newPath + " already exists"); } foreach (XmlNode fileNode in node.SelectNodes("files/file")) { string fileToCreateName = Path.Combine(newPath, fileNode.Attributes["name"].Value); if (!overwrite && File.Exists(fileToCreateName)) { throw new Exception("The file " + fileToCreateName + " already exists"); } bool isText = fileNode.Attributes["type"] != null ? fileNode.Attributes["type"].Value == "text" : false; if (isText) { using (StreamWriter sw = new StreamWriter(fileToCreateName)) { sw.Write(fileNode.InnerText); sw.Close(); } } else { string base64 = fileNode.InnerText; byte[] ba = Convert.FromBase64String(base64); using (FileStream fs = new FileStream(fileToCreateName, FileMode.Create, FileAccess.Write)) { fs.Write(ba, 0, ba.Length); fs.Close(); } } VersionStore.VersionFile(new FileInfo((fileToCreateName))); } foreach (XmlNode folderNode in node.SelectNodes("folders/folder")) { ProcessFolderNode(folderNode, newPath, overwrite); } }
private static void ProcessFolderNode(XmlNode node, string basePath, bool overwrite, Package pkg, bool root) { string unpackTo = HttpContext.Current.Server.MapPath(VirtualPathUtility.ToAbsolute("~" + basePath)); if (!root && node.Attributes["name"] != null) { string folderName = node.Attributes["name"].Value; basePath = Path.Combine(basePath, folderName); unpackTo = Path.Combine(unpackTo, folderName); } DirectoryInfo di = new DirectoryInfo(unpackTo); if (!di.Exists) { di.Create(); if (pkg.Directories == null) { pkg.Directories = new List <String>(); } pkg.Directories.Add(unpackTo); } foreach (XmlNode fileNode in node.SelectNodes("files/file")) { string fileToCreateName = Path.Combine(unpackTo, fileNode.Attributes["name"].Value); string pkgFileName = basePath.Replace("\\", "/") + "/" + fileNode.Attributes["name"].Value; pkgFileName = pkgFileName.Replace("//", "/"); Package p = IsFileOkToInstall(pkgFileName); if (p != null) { RemoveFilesAndFolders(pkg); throw new Exception("Cannot install this package because the file " + fileNode.Attributes["name"].Value + " is in use by the package <strong>" + p.Name + "</strong>."); } if (!overwrite && File.Exists(fileToCreateName)) { RemoveFilesAndFolders(pkg); throw new Exception("The file " + fileNode.Attributes["name"].Value + " already exists."); } if (overwrite && File.Exists(fileToCreateName)) { // if the file is a .dll, rename it. otherwise version it. if (fileToCreateName.ToLower().EndsWith(".dll")) { File.Move(fileToCreateName, fileToCreateName + ".old"); } else { VersionStore.VersionFile(new FileInfo(fileToCreateName)); } File.Delete(fileToCreateName); } if (pkg.Files == null) { pkg.Files = new List <String>(); } pkg.Files.Add(pkgFileName); try { WriteFile(fileToCreateName, fileNode.InnerText); } catch (Exception exc) { throw new Exception("Could not create file " + fileNode.Attributes["name"].Value + ". " + exc.Message); } } foreach (XmlNode folderNode in node.SelectNodes("folders/folder")) { ProcessFolderNode(folderNode, basePath, overwrite, pkg, false); } }