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; }
internal static void SetStatInfo(Entry entry, ref Stat stat) { stat.st_mode = NativeConvert.FromOctalPermissionString("0444"); switch (entry.Kind) { case EntryKind.Directory: case EntryKind.ZipDirectoryEntry: case EntryKind.ZipFile: stat.st_mode |= FilePermissions.S_IFDIR | NativeConvert.FromOctalPermissionString("0222"); stat.st_nlink = 2 + ((entry.Node != null) ? (ulong)GetEntriesCount(entry.Node, new EntryKind[] { EntryKind.Directory, EntryKind.ZipFile, EntryKind.ZipDirectoryEntry }) : 0); break; case EntryKind.File: stat.st_mode |= FilePermissions.S_IFLNK | NativeConvert.FromOctalPermissionString("0777"); stat.st_nlink = 1; break; case EntryKind.ZipFileEntry: stat.st_mode |= FilePermissions.S_IFREG; stat.st_nlink = 1; break; } stat.st_size = entry.Size; stat.st_mtime = NativeConvert.FromDateTime(entry.LastWriteTime); }
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; }
private static Entry ParseEntry(ICSharpCode.SharpZipLib.Zip.ZipEntry entry, Node root) { if ((!entry.IsFile) && (!entry.IsDirectory)) return null; string name = entry.Name.TrimEnd('/'); Node dir = GetDirectory(name.Substring(0, Math.Max(0, name.LastIndexOf('/'))), root); Entry newEntry = new Entry(entry); if (newEntry.Kind == EntryKind.ZipDirectoryEntry) newEntry.Node = new Node(); if (dir == null) return newEntry; dir.Nodes.Add(newEntry); return null; }
public static void Update(System.IO.DirectoryInfo directory, Node node) { IDictionary<string, Entry> entries = new Dictionary<string, Entry>(node.Nodes.Count); foreach (var entry in node.Nodes) { entries.Add(entry.Name, entry); } foreach (var subdirectory in directory.EnumerateDirectories()) { if (entries.ContainsKey(subdirectory.Name)) { entries.Remove(subdirectory.Name); } else { Entry dirEntry = new Entry(subdirectory); node.Nodes.Add(dirEntry); } } foreach (var file in directory.EnumerateFiles()) { if (entries.ContainsKey(file.Name)) { Entry fileEntry = entries[file.Name]; entries.Remove(file.Name); if (fileEntry.LastWriteTime < file.LastWriteTime) { node.Nodes.Remove(fileEntry); node.Nodes.Add(new Entry(file, IsZipFile(file))); } } else { node.Nodes.Add(new Entry(file, IsZipFile(file))); } } foreach (var entryPair in entries) { node.Nodes.Remove(entryPair.Value); } }
public static Entry CreateZipDirectoryEntry(string name, DateTime date) { Entry e = new Entry(); e.Name = name; e.LastWriteTime = date; e.Kind = EntryKind.ZipDirectoryEntry; e.Node = null; e.Size = 0; return e; }