How to create a file containing files and directories?

前端 未结 2 785
时光说笑
时光说笑 2021-01-07 15:50

I am working on a WPF open source markdown editor and I am at the point where I need to save and load documents.

A \"document\" contains 3 files:

  • A XML
相关标签:
2条回答
  • 2021-01-07 16:29

    You were right to consider zipping the files (you don't want to reinvent that wheel), and you were also right that unzipping to the filesystem is ugly. But you don't have to; you can create the zip and pull files out of it entirely in your own code with the ZipArchive class.

    0 讨论(0)
  • 2021-01-07 16:41

    If anyone is wondering how I applied Ed Plunkett's answer, here is the code:

    var saveDialog = new SaveFileDialog
    {
        CreatePrompt = true,
        OverwritePrompt = true,
        Filter = "Project Markdown File | *.pmd"
    };
    
    var result = saveDialog.ShowDialog();
    
    if (result != null)
    {
        if (result == true)
        {
            if (!Directory.Exists(saveDialog.FileName + "_temp"))
            {
                var parentFolder = Directory.CreateDirectory(saveDialog.FileName + "_temp").FullName;
    
                var mp = new MarkdownParser();
                // Generate HTML
                var html = mp.Parse(document.Markdown.Markdown);
    
                var markdownFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".md";
                var htmlFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".html";
                var metadataFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".xml";
                // Generate MD file
                using (var sw = new StreamWriter(markdownFilePath))
                {
                    sw.Write(document.Markdown.Markdown);
                }
                // Generate HTML file
                using (var sw = new StreamWriter(htmlFilePath))
                {
                    sw.Write(html);
                }
                // Generate XML file
                document.Metadata.FileName = saveDialog.SafeFileName;
                var gxs = new GenericXmlSerializer<DocumentMetadata>();
                gxs.Serialize(document.Metadata, metadataFilePath);
                // Generate style
                var cssFilePath = AppDomain.CurrentDomain.BaseDirectory + "Styles\\github-markdown.css";
                if (!Directory.Exists(parentFolder + "\\Styles"))
                {
                    Directory.CreateDirectory(parentFolder + "\\Styles");
                }
    
                if (!File.Exists(parentFolder + "\\Styles\\github-markdown.css"))
                {
                    File.Copy(cssFilePath, parentFolder + "\\Styles\\github-markdown.css");
                }
                // Generate the package
                ZipFile.CreateFromDirectory(parentFolder, saveDialog.FileName);
                // Update the view
                var saveResult = new SaveResult
                {
                    FileName = saveDialog.SafeFileName,
                    Source = htmlFilePath.ToUri(),
                    TempFile = saveDialog.FileName + "_temp"
                };
                return saveResult;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题