开发者

How to create file of files?

class Node
{
    FooType Data; // I can save Data to file with extension .foo
    void Save()
    {
       // save Data to .foo file
    }
}

Now ,

class Graph
{
    List<Node> Nodes; 
    void Save()
    {
        foreach(Node node in Nodes)
        {
            node.Save();
        }
    }
}

Now when I invoke someGraph.Save(); then it creates Nodes.Count number of files.

I would like 开发者_JS百科to appear those files as one file with extension somename.graph and be able to read it again as Nodes.

How can I do this? Is there a way to bundle files into a single different file?


Use an archive format like zip.

Without external library support, you could just use .Net's serialisation mechanism to store a Dictionary<string, byte[]> (filename and data for each file) in a single file. Start here.


Or Graph.Save should pass the file stream it is using to each of the node.Save calls, so Node.Save uses the same file stream as its enclosing Graph.


Can you not use the classes in the System.IO.Compression namespace? Specifically, GZipStream? This would not constitute a 3rd party library as it is built into .Net and is available from 2.0 and onward.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜