private static StringBuilder PPIndexRecursionHelper( ZipDirectory curdir, StringBuilder builder, string curpath = "", bool addDirName = false) { if (addDirName) { curpath = $"{curpath}{curdir.Name}/"; } else { curpath = $"{curpath}/"; } builder.AppendLine($"DIR {curpath}"); foreach (ZipFile zfile in curdir.Files) { builder.AppendLine($"FILE {curpath}{zfile.Name}"); } foreach (ZipDirectory zdir in curdir.Directories) { builder = PPIndexRecursionHelper(zdir, builder, curpath, true); } return(builder); }
public static void Unzip(IEnumerable <string> paths, string location) { foreach (string path in paths) { ConsoleUtil.PrintMessage($"Unzipping {path}"); ConsoleUtil.PrintMessage("Parsing index..."); ZipDirectory rootDir = Index(path); if (rootDir == null) { continue; } using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open)) { ConsoleUtil.PrintMessage("Unpacking..."); try { UnpackDirectory(rootDir, mmf, location); } catch (CorruptionException) { ConsoleUtil.PrintError("Unable to unzip because the .czip file is corrupt"); } } } }
public static ZipDirectory PackDirectory(DirectoryInfo dirinfo) { ConsoleUtil.PrintInfo($"Packing directory {dirinfo.FullName}"); ZipDirectory zdir = new ZipDirectory { Name = dirinfo.Name }; try { foreach (DirectoryInfo di in dirinfo.GetDirectories()) { ZipDirectory ndir = PackDirectory(di); if (ndir != null) { zdir.Directories.Add(ndir); } } foreach (FileInfo fi in dirinfo.GetFiles()) { ZipFile zfile = PackFile(fi); if (zfile != null) { zdir.Files.Add(zfile); } } return(zdir); } catch (UnauthorizedAccessException) { ConsoleUtil.PrintWarning($"Skipping {dirinfo.FullName} because access was denied"); return(null); } }
public void OpenCzip(string path) { ZipDirectory root = Api.Index(path); if (root == null) { return; // TODO: Handle this } TreeNode tn = new TreeNode(); AddRecursive(root, tn); tv_main.Nodes.Add(tn.Nodes[0]); }
public static void Unzip(IEnumerable <string> paths, IEnumerable <string> selectors, string location) { foreach (string path in paths) { ZipDirectory rootDir = Index(path); if (rootDir == null) { continue; } using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open)) { foreach (string selector in selectors) { ConsoleUtil.PrintInfo($"Searching for selector: {selector}"); IZippable zip = SelectorSearch( rootDir, selector.Split( new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries)); try { if (zip is ZipDirectory zdir) { ConsoleUtil.PrintMessage( $"Unpacking directory {zdir.Name} from {path}"); UnpackDirectory(zdir, mmf, location); } else if (zip is ZipFile zfile) { ConsoleUtil.PrintMessage( $"Unpacking file {zfile.Name} from {path}"); UnpackFile(zfile, mmf, location); } else { ConsoleUtil.PrintWarning( $"Selector {selector} not found in {path}"); } } catch (CorruptionException) { ConsoleUtil.PrintError( "Unable to unzip because the .czip file is corrupt"); } } } } }
public static void UnpackDirectory( ZipDirectory dir, MemoryMappedFile mmf, string dirPath) { string path = $"{dirPath}\\{dir.Name}"; ConsoleUtil.PrintInfo($"Unpacking directory {dir.Name}"); DirectoryInfo di; try { di = new DirectoryInfo(path); } catch (Exception ex) when(ex is ArgumentException || ex is PathTooLongException) { throw new CorruptionException($"Directory name \"{dir.Name}\" is invalid"); } if (File.Exists(path)) { if (ConsoleUtil.PromptYN("A file with the same name as the " + $"directory \"{di.FullName}\" being unpacked already exists, remove it?")) { try { File.Delete(path); } catch (Exception ex) when(ex is IOException || ex is UnauthorizedAccessException) { ConsoleUtil.PrintError("Unable delete file."); return; } } else { return; } } di.Create(); foreach (ZipDirectory subdir in dir.Directories) { UnpackDirectory(subdir, mmf, di.FullName); } foreach (ZipFile file in dir.Files) { UnpackFile(file, mmf, di.FullName); } }
public static string PPIndex(IEnumerable <string> paths) { StringBuilder res = new StringBuilder(); foreach (string path in paths) { ZipDirectory rootDir = Index(path); if (rootDir == null) { continue; } res.AppendLine($"{Environment.NewLine}Index of {path}:"); res = PPIndexRecursionHelper(rootDir, res); } return(res.ToString()); }
public void AddRecursive(ZipDirectory zdir, TreeNode root) { TreeNode tn = new TreeNode(zdir.Name, 0, 0); tn.Tag = zdir; root.Nodes.Add(tn); foreach (ZipDirectory dir in zdir.Directories) { AddRecursive(dir, tn); } foreach (ZipFile zfile in zdir.Files) { TreeNode ftn = new TreeNode(zfile.Name, 2, 2); ftn.Tag = zfile; tn.Nodes.Add(ftn); } }
public static void Zip(IEnumerable <string> paths, string location) { ZipDirectory rootDir = new ZipDirectory { Name = Path.GetFileName(paths.First().TrimEnd('\\', '/')) }; foreach (string path in paths) { ConsoleUtil.PrintMessage($"Zipping {path}"); FileInfo fi = new FileInfo(path); if (fi.Exists) { ZipFile zfile = PackFile(fi); if (zfile != null) { rootDir.Files.Add(zfile); } continue; } DirectoryInfo di = new DirectoryInfo(path); if (di.Exists) { ZipDirectory zdir = PackDirectory(di); if (zdir != null) { rootDir.Directories.Add(zdir); } } } long offset = rootDir.Serialize().Length; rootDir.OffsetFiles(offset); using (FileStream fs = File.Create(location)) { SerializedData sd = rootDir.Serialize(); sd.CopyTo(fs); foreach (ZipFile zfile in rootDir.AllFiles()) { zfile.CopyToFile(fs); } ConsoleUtil.PrintMessage($"Created .czip file {location}"); } }
public static ZipDirectory Index(string path) { using (BinaryReader stream = new BinaryReader( File.OpenRead(path), Encoding.Default)) { try { ConsoleUtil.PrintInfo($"Parsing index of {path}"); ZipDirectory zdir = IndexParser.Parse(stream); return(zdir); } catch (ParseException) { ConsoleUtil.PrintError($"Unable to parse index of {path}"); return(null); } } }
private static IZippable SelectorSearch(ZipDirectory root, IEnumerable <string> selector) { if (selector.Count() == 0) { return(root); } ConsoleUtil.PrintInfo($"Searching for {selector.First()} in {root.Name}"); IZippable zip = root.FindByName(selector.First()); if (selector.Count() == 1) { return(zip); } if (zip is ZipDirectory zdir) { return(SelectorSearch(zdir, selector.Skip(1))); } return(null); }
private static ZipDirectory ParseDirectory(BinaryReader stream) { char curChar; StringBuilder curField = new StringBuilder(); ZipDirectory zdir = new ZipDirectory(); // Parse Name while (true) { curChar = (char)stream.Read(); if (curChar == GS) { zdir.Name = curField.ToString(); curField.Clear(); break; } else if (stream.BaseStream.Position >= stream.BaseStream.Length) { throw new ParseException("End of stream while parsing"); } curField.Append(curChar); } // Parse Files while (stream.Read() == RS) { ZipFile nzfile = ParseFile(stream); zdir.Files.Add(nzfile); } // Parse Directories while (stream.Read() == RS) { ZipDirectory nzdir = ParseDirectory(stream); zdir.Directories.Add(nzdir); } ConsoleUtil.PrintInfo( $"Parsed directory \"{zdir.Name ?? "UNKNOWN NAME"}\" with " + $"{zdir.Directories.Count} subdirectories and {zdir.Files.Count} files"); return(zdir); }
public static ZipDirectory Parse(BinaryReader stream) { ZipDirectory root = ParseDirectory(stream); return(root); }