예제 #1
0
        protected void OnGetSnapshotClicked(object sender, EventArgs e)
        {
            var builder = new StringBuilder();
            using (CmsContext.Editing)
            {
                using (var sw = new StringWriter(builder))
                using (var writer = new XmlTextWriter(sw))
                {
                    var options = new SnapShotOptions();
                    options.From = PackagePreferences.ParseDateTime(txtDate.Text);
                    options.To = DateTime.Now;
                    options.ValidFileExtensions = cblExtensions.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value).ToArray();
                    CmsService.Instance.BuildSnapShot(options, writer);
                }
            }

            var snapshot = new XmlDocument();
            snapshot.LoadXml(builder.ToString());

            TreeView1.Nodes.Clear();

            var preferences = PackagePreferences.Load();

            AddFiles(snapshot, preferences);
            AddDataTypes(snapshot, preferences);
            AddTemplates(snapshot, preferences);
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/zip";

            var name = string.IsNullOrEmpty(context.Request.QueryString["name"]) ? "Package" : context.Request.QueryString["name"];
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileNameWithoutExtension(name).Replace(" ", "") + ".zip");

            var preferences = PackagePreferences.Load();

            var options = new SnapShotOptions();
            options.From = preferences.From;
            options.To = preferences.To;
            options.InvalidPaths = preferences.InvalidPaths;
            options.ValidFileExtensions = preferences.ValidFileExtensions;

            var snapshot = GenerateSnapshotXml(options);

            var doc = new XmlDocument();
            doc.LoadXml(snapshot);
            var files = doc.SelectNodes("snapshot/files/file").Cast<XmlElement>().Select(e => e.InnerText).ToArray();
            var applicationPath = context.Server.MapPath("~/");

            using (var zipfile = new ZipFile(Encoding.UTF8))
            {
                zipfile.AddEntry("snapshot.xml", snapshot);

                foreach (var file in files)
                {
                    var archivePath = "Files\\" + Path.GetDirectoryName(file).Substring(applicationPath.Length).TrimEnd('\\');
                    zipfile.AddFile(file, archivePath);
                }

                zipfile.Save(context.Response.OutputStream);
            }
        }
 private static string GenerateSnapshotXml(SnapShotOptions options)
 {
     var builder = new StringBuilder();
     using (CmsContext.Editing)
     {
         using (var sw = new StringWriter(builder))
         using (var writer = new XmlTextWriter(sw))
         {
             CmsService.Instance.BuildSnapShot(options, writer);
         }
     }
     return builder.ToString();
 }