简体   繁体   English

如何创建包含文件和目录的文件?

[英]How to create a file containing files and directories?

I am working on a WPF open source markdown editor and I am at the point where I need to save and load documents. 我正在使用WPF 开源Markdown编辑器 ,现在需要保存和加载文档。

A "document" contains 3 files: 一个“文档”包含3个文件:

  • A XML file for metadata 用于元数据的XML文件
  • A MD file for the markdown text 降价文字的MD文件
  • A HTML file for the HTML view of the document. 用于文档的HTML视图的HTML文件。

At this point for testing I create a directory for each document to hold these files, however this is not very convenient. 在测试时,我为每个文档创建一个目录来保存这些文件,但这不是很方便。 I need all those files to be packaged inside a single file so users wouldn't be confused with 3 separate files. 我需要将所有这些文件打包在一个文件中,这样用户才不会与3个单独的文件混淆。

I did a bit of research and found out office documents have a similar structure where each file is actually a package that holds multiple directories and files. 我做了一些研究,发现办公文档的结构类似,其中每个文件实际上都是一个包含多个目录和文件的软件包。 To open such an office document all you need to do change the extension to ZIP and you can browse the files, but on the outside it looks as a single file. 要打开这样的Office文档,您只需要将扩展​​名更改为ZIP即可浏览文件,但是在外部,它看起来像是一个文件。

My initial idea was to zip all the files to a single file, change extension when saving. 我最初的想法是将所有文件压缩为一个文件,在保存时更改扩展名。 And unzip it when I am actually loading the file. 并在我实际加载文件时将其解压缩。 But I felt this approach was not very elegant. 但是我觉得这种方法不是很优雅。

Is there any elegant way to archive multiple files and directories into a single file and access them I need them in C#? 有没有什么优雅的方法可以将多个文件和目录归档到一个文件中,并在C#中访问它们,我需要它们?

Any advice would be appreciated. 任何意见,将不胜感激。

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 . 您可以使用ZipArchive类在自己的代码中创建zip并将文件完全拉出。

If anyone is wondering how I applied Ed Plunkett's answer, here is the code: 如果有人想知道我如何应用Ed Plunkett的答案,请参见以下代码:

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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM