public static Node Generate(System.IO.DirectoryInfo directory, bool recursive) { Node node = new Node(); foreach (var subdirectory in directory.EnumerateDirectories()) { Entry dirEntry = new Entry(subdirectory); node.Nodes.Add(dirEntry); if (recursive) { dirEntry.Node = Generate(subdirectory, recursive); dirEntry.UpdateSize(); } } foreach (var file in directory.EnumerateFiles()) { Entry fileEntry = new Entry(file, IsZipFile(file)); node.Nodes.Add(fileEntry); if ((fileEntry.Kind == EntryKind.ZipFile) && recursive) { fileEntry.Node = ZipFileGenerator.Generate(file); fileEntry.UpdateSize(); } } return(node); }
private static Entry SearchEntry(string path, Node parent, string rootPath, EntryKind?stopAt) { Entry result = null; string[] pathComponents = path.Split(System.IO.Path.DirectorySeparatorChar); Node currentNode = parent; for (int i = 0; i < pathComponents.Length; i++) { result = null; foreach (var entry in currentNode.Nodes) { if (entry.Name == pathComponents[i]) { result = entry; break; } } if (result == null) { break; } if (((result.Kind == EntryKind.File) || (result.Kind == EntryKind.ZipFileEntry)) && (i + 1 < pathComponents.Length)) { throw new System.IO.DirectoryNotFoundException(); } if (result.Node == null) { if (rootPath == null) { throw new System.IO.IOException(); } string elementPath = System.IO.Path.Combine(rootPath, string.Join(new string(System.IO.Path.DirectorySeparatorChar, 1), pathComponents, 0, i + 1)); if (result.Kind == EntryKind.Directory) { result.Node = DirectoryGenerator.Generate(new System.IO.DirectoryInfo(elementPath), false); result.UpdateSize(); } if (result.Kind == EntryKind.ZipFile) { result.Node = ZipFileGenerator.Generate(new System.IO.FileInfo(elementPath)); result.UpdateSize(); } } else if (rootPath != null) { string elementPath = System.IO.Path.Combine(rootPath, string.Join(new string(System.IO.Path.DirectorySeparatorChar, 1), pathComponents, 0, i + 1)); if (result.Kind == EntryKind.Directory) { if (result.LastWriteTime < System.IO.Directory.GetLastWriteTime(elementPath)) { System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(elementPath); DirectoryGenerator.Update(di, result.Node); Entry newEntry = new Entry(di); newEntry.Node = result.Node; newEntry.UpdateSize(); currentNode.Nodes.Remove(result); currentNode.Nodes.Add(newEntry); result = newEntry; } } if (result.Kind == EntryKind.File) { if (result.LastWriteTime < System.IO.File.GetLastWriteTime(elementPath)) { System.IO.FileInfo fi = new System.IO.FileInfo(elementPath); Entry newEntry = new Entry(fi, false); currentNode.Nodes.Remove(result); currentNode.Nodes.Add(newEntry); result = newEntry; } } } if (stopAt.HasValue && (result.Kind == stopAt.Value)) { break; } currentNode = result.Node; } if ((result != null) && stopAt.HasValue && (result.Kind != stopAt.Value)) { return(null); } return(result); }