/// <summary> /// Adds the package passed to the entry hierarchy, creating folders /// as necessary. /// </summary> /// <param name="entry">The new entry.</param> private void AddEntry(PackageEntry entry) { PackageEntry parent; string path; int pos, posEnd; // Make sure that all of the folders up // the hierarchy exist. parent = root; pos = 1; while (true) { posEnd = entry.FullName.IndexOf('/', pos); if (posEnd == -1) { break; } path = entry.FullName.Substring(0, posEnd); parent = this[path]; if (parent != null && !parent.IsFolder) { throw new PackageException(MustBeFolder, path); } else if (parent == null) { parent = AddFolder(path); } pos = posEnd + 1; } entry.SetParent(parent); parent.AddChild(entry); entries.Add(entry.FullName.ToUpper(), entry); }
/// <summary> /// Scans the input stream, validating its structure and loading /// the package entries. /// </summary> private void Load() { PackageHeader header; byte[] hash; packageIn.Position = 0; header = new PackageHeader(packageIn); cbHeader = (int)packageIn.Position; // Compute a MD5 hash on the rest of then file and compare // the result to what we read from the header. hash = MD5Hasher.Compute(packageIn, packageIn.Length - cbHeader); if (hash.Length != header.Hash.Length) { throw new PackageException(CorruptPackage); } for (int i = 0; i < hash.Length; i++) { if (hash[i] != header.Hash[i]) { throw new PackageException(CorruptPackage); } } // Read the package entry headers. packageIn.Position = cbHeader; while (true) { var entry = new PackageEntry(this, packageIn); if (entry.IsEol) { break; } entries.Add(entry.FullName.ToUpper(), entry); } // Walk back through the entries and link each entry to // its parent folder. foreach (PackageEntry entry in entries.Values) { PackageEntry parent; int pos; if (entry == root) { continue; } pos = entry.FullName.LastIndexOf('/'); if (pos == -1) { throw new PackageException(CorruptPackage); } if (pos == 0) { parent = root; } else { parent = (PackageEntry)entries[entry.FullName.Substring(0, pos).ToUpper()]; if (parent == null) { throw new PackageException(CorruptPackage); } } entry.SetParent(parent); parent.AddChild(entry); } }