Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ContentArchiveNode"/> class from an archive stream.
        /// </summary>
        /// <param name="parent">The node's parent node.</param>
        /// <param name="reader">A <see cref="BinaryReader"/> on the stream containing the archive data.</param>
        private ContentArchiveNode(ContentArchiveNode parent, BinaryReader reader)
        {
            this.parent      = parent;
            this.name        = reader.ReadString();
            this.path        = BuildPath(parent, name);
            this.isFile      = reader.ReadBoolean();
            this.isDirectory = !this.isFile;

            if (this.isFile)
            {
                this.position    = reader.ReadInt64();
                this.sizeInBytes = reader.ReadInt64();
            }
            else
            {
                var childList = new List<ContentArchiveNode>();
                var childCount = reader.ReadInt32();

                for (int i = 0; i < childCount; i++)
                {
                    var node = new ContentArchiveNode(this, reader);
                    childList.Add(node);
                }

                this.children = childList;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ContentArchiveNode"/> class from a file or directory in the file system.
        /// </summary>
        /// <param name="parent">The node's parent node.</param>
        /// <param name="path">The path to the file or directory that this node represents.</param>
        private ContentArchiveNode(ContentArchiveNode parent, String path)
        {
            path = System.IO.Path.GetFullPath(path);

            this.parent      = parent;
            this.path        = path;
            this.name        = System.IO.Path.GetFileName(path);
            this.isFile      = File.Exists(path);
            this.isDirectory = Directory.Exists(path);

            if (!isFile && !isDirectory)
                throw new FileNotFoundException(path);

            if (isFile)
            {
                using (var stream = File.OpenRead(path))
                {
                    this.sizeInBytes = stream.Length;
                }
            }
            else
            {
                LoadChildren(path);
            }
        }